Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Generic Rule to Redirect and Rewrite URL using .htaccess

Posted on 2016-09-21
8
85 Views
Last Modified: 2016-09-24
Hi,

I want to redirect and then rewrite my URL (internal forwarding) so that it doesn't show the php extension as well it doesn't show the ?, = and & in the URL.

The solution for that was provided for a single script in the question https://www.experts-exchange.com/questions/28969579/Redirect-and-Rewrite-URL-using-htaccess.html by Terry Woods.

In this question i want to achieve the same but instead of adding Rewrite Rules for every script individually i just add one generic rule which applies to all the scripts. Below is the example.

The URL http://localhost/portal/SCRIPTNAME.php?var1=val1 be redirected i.e. show in address bar as http://localhost/portal/SCRIPTNAME.php/var1/val1 and internally it is forwarded to the actual URL http://localhost/portal/SCRIPTNAME.php?var1=val1

The SCRIPTNAME.php can be any name for ex. "error.php" or "admin.php" etc. as well as there can be multiple $_GET variables in a single URL for ex: http://localhost/portal/SCRIPTNAME.php?var1=val1&var2=val2&val3=val3 must be shown in address bar as http://localhost/portal/SCRIPTNAME.php/var1/val1/var2/val2/val3/val3

Also there is no pattern in the naming of the $_GET variables or values.

Kindly let me know if any more information is required

Thanks in advance
0
Comment
Question by:Vipin Kumar
  • 4
  • 3
8 Comments
 

Expert Comment

by:jdmailny
ID: 41809947
Replace php with a host name
0
 
LVL 1

Author Comment

by:Vipin Kumar
ID: 41809965
@jdmailny,

I did not understand your solution, can you provide bit more information.

Thanks
0
 
LVL 35

Expert Comment

by:Terry Woods
ID: 41810481
I think this can probably be done. I'll try to have a look at it tomorrow.
0
Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

 
LVL 35

Assisted Solution

by:Terry Woods
Terry Woods earned 500 total points
ID: 41812330
This seems to work for me:

RewriteCond %{QUERY_STRING} !redirect=done
RewriteCond %{QUERY_STRING} ^((?!redirect)\w+)=([^&]+)(?:&(.*))? [NC]
RewriteRule ^([^/]*/\w+\.php)(/[\w/]+)? $1$2/%1/%2?%3 [NC,R=302,L]

RewriteCond %{QUERY_STRING} ^((?:(?!&?redirect=done).)*) [NC]
RewriteRule ^([^/]*/\w+\.php)([^?]*/)([^/?]+)/([^/?]+) $1$2?$3=$4&%1&redirect=done [NC,L]

Open in new window


Loading URL:
http://localhost/portal/script.php?error=foo¶m2=blah¶m3=rhubarb
Redirects me to:
http://localhost/portal/script.php/error/foo/param2/blah/param3/rhubarb

And gave this result of $_GET parameters from my portal/script.php ($_SERVER['PHP_SELF'] is output in the first line):
/portal/script.php/

Array
(
    [error] => foo
    [param2] => blah
    [param3] => rhubarb
    [redirect] => done
)

Open in new window

0
 
LVL 35

Expert Comment

by:Terry Woods
ID: 41812333
I know some of those regex patterns are pretty hairy... explaining them might be worthy of a separate question though!
0
 
LVL 1

Author Comment

by:Vipin Kumar
ID: 41813457
@Terry,

Doesn't work. Nothing is happening Neither Redirection nor internal redirection

Thanks
0
 
LVL 35

Accepted Solution

by:
Terry Woods earned 500 total points
ID: 41813483
Which folder are you putting the .htaccess file in? You may need to add the rewritebase option (set to /portal/ ) like you used in the previous question.

In the previous question, we relaxed the pattern for the PHP file. We could try that here too, like this:

RewriteCond %{QUERY_STRING} !redirect=done
RewriteCond %{QUERY_STRING} ^((?!redirect)\w+)=([^&]+)(?:&(.*))? [NC]
RewriteRule ^(.+\.php)(/[\w/]+)? $1$2/%1/%2?%3 [NC,R=302,L]

RewriteCond %{QUERY_STRING} ^((?:(?!&?redirect=done).)*) [NC]
RewriteRule ^(.+\.php)([^?]*/)([^/?]+)/([^/?]+) $1$2?$3=$4&%1&redirect=done [NC,L]

Open in new window

0
 
LVL 1

Author Closing Comment

by:Vipin Kumar
ID: 41813730
@Terry,

Works perfectly fine.

Thanks
0

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
This video teaches users how to migrate an existing Wordpress website to a new domain.
The viewer will learn how to dynamically set the form action using jQuery.

829 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question