Solved

Generic Rule to Redirect and Rewrite URL using .htaccess

Posted on 2016-09-21
8
89 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
[X]
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
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Learn by example how to specify CSS selectors for Selenium WebDriver test automation software.
Get to know the ins and outs of building a web-based ERP system for your enterprise. Development timeline, technology, and costs outlined.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

730 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