.htaccess Rewrite; but Variable needed - php

Hello Stackers,
I Currently have a Problem with my HTACCESS file. It needs to rewrite a Url, which it does, but I still need a Variable to pass through.
RewriteRule ^ref/(.*)$ /quickregister/start.php?ref=$1
The Link used is example.com/ref/value however, after the rewrite, no variables are recognizable, but I need the value of the REF variable.
Is there A way to do this? Also, I would preffer to still get if(isset($_GET['ref'])){ working.

This is my standard .htaccess file which I use for all of my rewrite websites...
## No directory listings
IndexIgnore *
## Can be commented out if causes errors, see notes above.
Options +FollowSymlinks
Options -Indexes
## Mod_rewrite in use.
RewriteEngine On
# Block out any script trying to base64_encode data within the URL.
RewriteCond %{QUERY_STRING} base64_encode[^(]*\([^)]*\) [OR]
# Block out any script that includes a <script> tag in URL.
RewriteCond %{QUERY_STRING} (<|%3C)([^s]*s)+cript.*(>|%3E) [NC,OR]
# Block out any script trying to set a PHP GLOBALS variable via URL.
RewriteCond %{QUERY_STRING} GLOBALS(=|\[|\%[0-9A-Z]{0,2}) [OR]
# Block out any script trying to modify a _REQUEST variable via URL.
RewriteCond %{QUERY_STRING} _REQUEST(=|\[|\%[0-9A-Z]{0,2})
# Return 403 Forbidden header and show the content of the root homepage
RewriteRule .* index.php [F]
#
## End - Rewrite rules to block out some common exploits.
## Begin - Custom redirects
#
# If you need to redirect some pages, or set a canonical non-www to
# www redirect (or vice versa), place that code here. Ensure those
# redirects use the correct RewriteRule syntax and the [R=301,L] flags.
#
## End - Custom redirects
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteCond %{REQUEST_URI} !^/index\.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php [L]
Using this file everything in the URL if it is not a valid directory or file is ignored and routed to index.php in the site root. I then access it as a single paramater using $_SERVER['REQUEST_URI'] which I can explode using explode("/", $_SERVER['REQUEST_URI']), this gives me an array starting at index 0 of each value. Using this script I am still able to access $_GET variables as long as at the end of the URI the convention ?var_name=value has been used.
This has been tested and works with Apache, PHP5 and works both debian based linux operating systems as well as Windows installs of Apache.

Related

301 Redirect via .htaccess isn't working

I have about 50 old links which is to be redirected to new links. However, instead of re-directing, I'm sent to 404 page.
This is my htaccess file below. Any idea what I'm doing wrong?
## Mod_rewrite in use.
RewriteEngine On
## Begin - Rewrite rules to block out some common exploits.
# If you experience problems on your site then comment out the operations listed
# below by adding a # to the beginning of the line.
# This attempts to block the most common type of exploit `attempts` on Joomla!
#
# Block any script trying to base64_encode data within the URL.
RewriteCond %{QUERY_STRING} base64_encode[^(]*\([^)]*\) [OR]
# Block any script that includes a <script> tag in URL.
RewriteCond %{QUERY_STRING} (<|%3C)([^s]*s)+cript.*(>|%3E) [NC,OR]
# Block any script trying to set a PHP GLOBALS variable via URL.
RewriteCond %{QUERY_STRING} GLOBALS(=|\[|\%[0-9A-Z]{0,2}) [OR]
# Block any script trying to modify a _REQUEST variable via URL.
RewriteCond %{QUERY_STRING} _REQUEST(=|\[|\%[0-9A-Z]{0,2})
# Return 403 Forbidden header and show the content of the root homepage
RewriteRule .* index.php [F]
#
## End - Rewrite rules to block out some common exploits.
## Begin - Custom redirects
#
# If you need to redirect some pages, or set a canonical non-www to
# www redirect (or vice versa), place that code here. Ensure those
# redirects use the correct RewriteRule syntax and the [R=301,L] flags.
#
## End - Custom redirects
Redirect 301 http://domain.me/blog.php?id=7 http://domain.me/blog
Redirect 301 http://domain.me/iiblg/17-0-The-girl-with.html http://domain.me/blog/the-girl-with
Redirect 301 http://domain.me/iiblg/20-0-Sendra-Lake.html http://domain.me/blog/sendra-lake
There's many more links. I just removed it from here.
You can not match against querystring using Redirect directive. You need to use mod-rewrite to do that.
To redirect example.com/blog.php?id=7 to example.com/blog you need the following rule :
RewriteEngine on
RewriteCond %{QUERY_STRING} ^id=7$
RewriteRule ^blog\.php$ http://example.com/blog/? [NC,L,R]

How to redirect static to dynamic URL's using .htaccess (on Wordpress site)

I've taken over a former site/domain, and set up a new site using Wordpress. The WP installation rewrites URL's to static ones, as you'd expect it to.
At the same time I want to preserve the former pages, as they have incoming links. I'm not interested in 301'ing them to "new" pages.
The old URL structure is /index.php?id=123, which I suspect is causing the problem with the WP .htaccess file. For reference, this is what it looks like:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
I've tried adding the following:
RewriteRule ^([0-9]+).html index.php?id=$1 [R,L]
Doesn't work. Just redirects to site.com/?id=123 and shows the front page.
I should add that I plan on just adding these new pages as regular static HTML files in the format of 123.html, 321.html etc.
How do I use .htaccess to make this work together with the WP installation and what WP puts into the .htaccess file?
To clarify:
I want to have my 123.html static HTML page be index.php?id=123. When you access index.php?id=123 it should bring up 123.html, but show index.php?id=123 in the address bar. If you access 123.html it should 301 to index.php?id=123.
To map an URL with a querystring up to an actual file you'll need to use a RewriteCond to match the querystring itself (as RewriteRule doesn't):
Something along these lines ought to do it:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
# retrieve X.html when index.php?id=X is requested
RewriteCond %{REQUEST_FILENAME} index\.php
RewriteCond %{QUERY_STRING} ^id=([0-9]+)$
RewriteCond %1.html -F
RewriteRule .* %1.html? [L]
# standard WordPress routing
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
This will first check to see if you've got a request for index.php with a querystring like id=X.
Then it'll check to see if a file called X.html actually exists; I'm not 100% happy about having to use the more system hungry subrequest file check -F rather than the standard -f but I can't see a way around it in .htaccess in this case.
If X.html actually exists, it'll fetch that file whilst leaving the URL as index.php?id=X.
However if that file doesn't exist it'll fall back to standard WordPress no file, no directory routing to index.php
I'm not a WordPress expert but that should work; I guess the main WordPress controller uses $_SERVER['REQUEST_URI'] to determine the action.
Note: This won't, however, prevent people from accessing 123.html directly by going to the URL www.site.com/123.html - I kept falling into infinite loops and Apache 500 errors trying to prevent that :|

Redirect fails for .cfm aliases in htaccess in joomla

One of our old sites used .cfm files we attempted to redirect the specific pages of the old site to the new site in the redirect manager . For resolving this issue we changed htaccess into following.
RewriteCond %{REQUEST_URI} /component/|(/[^.]*|\.(php|html?|feed|pdf|vcf|raw|cfm))$ [NC]
This is working for url like
www.abc.com/viewCompanyProfile.cfm?cmpID=861
but this is not working for
www.abc.com/application5/viewCompanyProfile.cfm?cmpID=861
Any help will be appriciated.
Here is my htaccess detail.
## Begin - Rewrite rules to block out some common exploits.
# If you experience problems on your site block out the operations listed below
# This attempts to block the most common type of exploit `attempts` to Joomla!
#
# Block out any script trying to base64_encode data within the URL.
RewriteCond %{QUERY_STRING} base64_encode[^(]*\([^)]*\) [OR]
# Block out any script that includes a <script> tag in URL.
RewriteCond %{QUERY_STRING} (<|%3C)([^s]*s)+cript.*(>|%3E) [NC,OR]
# Block out any script trying to set a PHP GLOBALS variable via URL.
RewriteCond %{QUERY_STRING} GLOBALS(=|\[|\%[0-9A-Z]{0,2}) [OR]
# Block out any script trying to modify a _REQUEST variable via URL.
RewriteCond %{QUERY_STRING} _REQUEST(=|\[|\%[0-9A-Z]{0,2})
# Return 403 Forbidden header and show the content of the root homepage
RewriteRule .* index.php [F]
#
## End - Rewrite rules to block out some common exploits.
## Begin - Custom redirects
#
# If you need to redirect some pages, or set a canonical non-www to
# www redirect (or vice versa), place that code here. Ensure those
# redirects use the correct RewriteRule syntax and the [R=301,L] flags.
#
## End - Custom redirects
##
# Uncomment following line if your webserver's URL
# is not directly related to physical file paths.
# Update Your Joomla! Directory (just / for root).
##
# RewriteBase /
## Begin - Joomla! core SEF Section.
#
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
#
# If the requested path and file is not /index.php and the request
# has not already been internally rewritten to the index.php script
RewriteCond %{REQUEST_URI} !^/index\.php
# and the request is for something within the component folder,
# or for the site root, or for an extensionless URL, or the
# requested URL ends with one of the listed extensions
#RewriteCond %{REQUEST_URI} /component/|(/[^.]*|\.(php|html?|feed|pdf|vcf|raw))$ [NC]
RewriteCond %{REQUEST_URI} /component/|(/[^.]*|\.(php|html?|feed|pdf|vcf|raw|cfm))$ [NC]
# and the requested path and file doesn't directly match a physical file
RewriteCond %{REQUEST_FILENAME} !-f
# and the requested path and file doesn't directly match a physical folder
RewriteCond %{REQUEST_FILENAME} !-d
# internally rewrite the request to the index.php script
RewriteRule .* index.php [L]
#
## End - Joomla! core SEF Section.

Create Rewrite rule in htaccess for a user profile

So I have a dilemma..
Can't figure out how to rewrite this URL to THIS URL:
URL=www.EXAMPLE.com/users/USERNAME
THIS URL= www.EXAMPLE.com/USERNAME
This isn't my first time using Stack Overflow, but this is my first time actually needing an account to find my answer. I will Thank accordingly.
Thanks,
Nick
EDIT:
RewriteRule ^user/(.*)$ /$1 [R=301,L]
But I get a 404 page, weird I say.
The page redirects successfully, but with a 404 instead.
HTACCESS as follows:
## Mod_rewrite in use.
RewriteEngine On
RewriteRule ^user/(.*)$ /$1 [R=301,L]
## Begin - Rewrite rules to block out some common exploits.
# If you experience problems on your site block out the operations listed below
# This attempts to block the most common type of exploit `attempts` to Joomla!
#
# Block out any script trying to base64_encode data within the URL.
RewriteCond %{QUERY_STRING} base64_encode[^(]*\([^)]*\) [OR]
# Block out any script that includes a <script> tag in URL.
RewriteCond %{QUERY_STRING} (<|%3C)([^s]*s)+cript.*(>|%3E) [NC,OR]
# Block out any script trying to set a PHP GLOBALS variable via URL.
RewriteCond %{QUERY_STRING} GLOBALS(=|\[|\%[0-9A-Z]{0,2}) [OR]
# Block out any script trying to modify a _REQUEST variable via URL.
RewriteCond %{QUERY_STRING} _REQUEST(=|\[|\%[0-9A-Z]{0,2})
# Return 403 Forbidden header and show the content of the root homepage
RewriteRule .* index.php [F]
#
## End - Rewrite rules to block out some common exploits.
## Begin - Custom redirects
#
# If you need to redirect some pages, or set a canonical non-www to
# www redirect (or vice versa), place that code here. Ensure those
# redirects use the correct RewriteRule syntax and the [R=301,L] flags.
#
## End - Custom redirects
##
# Uncomment following line if your webserver's URL
# is not directly related to physical file paths.
# Update Your Joomla! Directory (just / for root).
##
# RewriteBase /
## Begin - Joomla! core SEF Section.
#
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
#
# If the requested path and file is not /index.php and the request
# has not already been internally rewritten to the index.php script
RewriteCond %{REQUEST_URI} !^/index\.php
# and the request is for something within the component folder,
# or for the site root, or for an extensionless URL, or the
# requested URL ends with one of the listed extensions
RewriteCond %{REQUEST_URI} /component/|(/[^.]*|\.(php|html?|feed|pdf|vcf|raw))$ [NC]
# and the requested path and file doesn't directly match a physical file
RewriteCond %{REQUEST_FILENAME} !-f
# and the requested path and file doesn't directly match a physical folder
RewriteCond %{REQUEST_FILENAME} !-d
# internally rewrite the request to the index.php script
RewriteRule .* index.php [L]
#
## End - Joomla! core SEF Section.
[UPDATE 4/08/13]
So after a while I have figured out that the only way an extension I am using works alongside Joomla! You need to use a separate extension called sh404sef to make this work. So in a nut shell, no use editing the HTACCESS file like I thought. Ended up spending the 40 bucks and it worked instantly. HAHA
This should answer your question
RewriteRule ^users/(.*)$ $1
Source: .htaccess rewrite from subdirectory to root
A site i find useful with .htaccess url rewriting is:
http://roshanbh.com.np/2008/03/url-rewriting-examples-htaccess.html
The example:
Rewriting yoursite.com/user.php?username=xyz to yoursite.com/xyz
Have you checked zorpia.com.If you type http://zorpia.com/roshanbh233
in browser you can see my profile over there. If you want to do the
same kind of redirection i.e http://yoursite.com/xyz to
http://yoursite.com/user.php?username=xyz then you can add the
following code to the .htaccess file.
RewriteEngine On
RewriteRule ^([a-zA-Z0-9_-]+)$ user.php?username=$1
RewriteRule ^([a-zA-Z0-9_-]+)/$ user.php?username=$1
So in your case you could go for:
RewriteRule ^users/([a-zA-Z0-9_-]+)$ user.php?username=$1
RewriteRule ^users/([a-zA-Z0-9_-]+)/$ user.php?username=$1
see https://stackoverflow.com/questions/20610980/mod-rewrite-redirect-except-parked-domain
All www.primary.com go to www.primary.com/webtrees
Options -Indexes +SymLinksIfOwnerMatch
RewriteEngine on
RewriteBase /
RewriteCond $1 !^parked.com/
RewriteCond $1 !^piwik/
RewriteCond $1 !^wordpress/
RewriteCond $1 !^marker.htm
RewriteCond %{REQUEST_URI} !^/webtrees/
RewriteRule ^(.*)$ /webtrees/$1 [L]

Restler returns 404 status code if index.php is not included in URL

First of all, thanks for RESTLER Framework, it is wonderful!
I've set up Restler API on "http://api.odience.net/" .
Restler responds with correct response body but returns a 404 status code if I don't include index.php in the url. I've set up the .htaccess file as said in the examples but I can't figure out why it doesn't reply with the correct status code.
Example:
*- Accessing /sandbox/about/products/en.json (and passing some GET variables + calling the about method of sandbox.php) returns a 404 header even if the body data returned is correct!
*- If we add the "index.php" file to the url, headers are fine!
Try:
Access /index.php/sandbox/about/products/en.json (with same GET vars)
Here is my detailed .HTACCESS file for the Restler root directory:
## Can be commented out if causes errors.
Options +FollowSymLinks
<IfModule mod_rewrite.c>
RewriteEngine On
## Begin - Rewrite rules to block out some common exploits.
# If you experience problems on your site block out the operations listed below
# This attempts to block the most common type of exploit `attempts` to Joomla!
#
# Block out any script trying to base64_encode data within the URL.
RewriteCond %{QUERY_STRING} base64_encode[^(]*\([^)]*\) [OR]
# Block out any script that includes a <script> tag in URL.
RewriteCond %{QUERY_STRING} (<|%3C)([^s]*s)+cript.*(>|%3E) [NC,OR]
# Block out any script trying to set a PHP GLOBALS variable via URL.
RewriteCond %{QUERY_STRING} GLOBALS(=|\[|\%[0-9A-Z]{0,2}) [OR]
# Block out any script trying to modify a _REQUEST variable via URL.
RewriteCond %{QUERY_STRING} _REQUEST(=|\[|\%[0-9A-Z]{0,2})
# Return 403 Forbidden header and show the content of the root homepage
RewriteRule .* index.php [F]
#
## End - Rewrite rules to block out some common exploits.
##
# Uncomment following line if your webserver's URL
# is not directly related to physical file paths.
# Update Your API Directory (just / for root).
##
RewriteBase /
#
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
#
# If the requested path and file is not /index.php and the request
# has not already been internally rewritten to the index.php script
RewriteCond %{REQUEST_URI} !^/index\.php
# and the request is for something within the reg server folder,
# or for the site root, or for an extensionless URL, or the
# requested URL ends with one of the listed extensions
RewriteCond %{REQUEST_URI} /server/|(/[^.]*|\.(php|html?|json|xml|feed|pdf|vcf|raw))$ [NC]
# and the requested path and file doesn't directly match a physical file
RewriteCond %{REQUEST_FILENAME} !-f
# and the requested path and file doesn't directly match a physical folder
RewriteCond %{REQUEST_FILENAME} !-d
# internally rewrite the request to the index.php script
RewriteRule ^.*$ index.php [QSA,L]
#
</IfModule>
To secure Restler framework, the Restler library files are not available to the web and the API_ROOT/index.php file includes them automatically with a "require_once" command.
Please advise how to make this setup work as expected?
Change %{REQUEST_FILENAME} in .htaccess file to %{DOCUMENT_ROOT}%{REQUEST_FILENAME} (it worked for me) Below is my .htaccess file content. HTH
RewriteEngine On
RewriteBase /
RewriteRule ^$ index.php [QSA,L]
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_FILENAME} !-f
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php [L,QSA]

Categories