Solved

Generic Rule to Redirect and Rewrite URL using .htaccess

Posted on 2016-09-21
8
103 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
Get Actionable Data from Your Monitoring Solution

Your communication platform is only as good as the relevance of the information you send. Ensure your alerts get to the right people every time with actionable responses. Create escalation rules that ensure everyone follows the process and nothing is left to chance.

 
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

Stressed Out?

Watch some penguins on the livecam!

Question has a verified solution.

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

Because your company can’t afford for you to make SEO mistakes, you’ll want to ensure you’re taking the right steps each and every time you post a new piece of content. This list of optimization do’s and don’ts can help you become an SEO wizard.
There’s a good reason for why it’s called a homepage – it closely resembles that of a physical house and the only real difference is that it’s online. Your website’s homepage is where people come to visit you. It’s the family room of your website wh…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

718 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