Solved

Generic Rule to Redirect and Rewrite URL using .htaccess

Posted on 2016-09-21
8
65 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
 
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&param2=blah&param3=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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Using SQL Scripts we can save all the SQL queries as files that we use very frequently on our database later point of time. This is one of the feature present under SQL Workshop in Oracle Application Express.
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
This tutorial demonstrates how to identify and create boundary or building outlines in Google Maps. In this example, I outline the boundaries of an enclosed skatepark within a community park.  Login to your Google Account, then  Google for "Google M…
Any person in technology especially those working for big companies should at least know about the basics of web accessibility. Believe it or not there are even laws in place that require businesses to provide such means for the disabled and aging p…

747 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now