Solved

Generic Rule to Redirect and Rewrite URL using .htaccess

Posted on 2016-09-21
8
81 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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

The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

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.
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 is a quite short video tutorial. In this video, I'm going to show you how to create self-host WordPress blog with free hosting service.

785 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