my .htaccess redirection fails - php

htaccess is enabled, i have the canonicalization running (no-www to www.)
I'm trying to use htaccess to do the following
www.domain.com/page.php?i=Page1
www.domain.com/page.php?i=Page2
To
www.domain.com/Page1
www.domain.com/Page2
I tried using this code snippet, with no luck so far:
rewriterule ^([a-zA-Z0-9_-]+)/$ page.php?i=$1
However I think I'm going the reverse way. I can't find example for this.
I have this but I can't make it work.

Try this:
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/page\.php$
RewriteCond %{QUERY_STRING} ^i=([a-zA-Z0-9_-]+)$
RewriteRule ^(.*)$ http://www.domain.com/%1? [R=302,L]
taken from: http://www.simonecarletti.com/blog/2009/01/apache-query-string-redirects/

You want www.domain.com/Page1 to be visible in the browser's address bar, but internally that url should actually /page.php?i=Page1. In that case the problem is the trailing / in your regex:
Rewriterule ^([a-zA-Z0-9_-]+)/$ page.php?i=$1
^---here
Your desired /Page1 url has no trailing slash, yet your rewrite regex requires one, so the pattern doesn't match, and no rewriting occurs. Try removing the / and see if that helps any:
Rewriterule ^([a-zA-Z0-9_-]+)$ page.php?i=$1
^---no /

In order to parse out the query string you need to access it in a RewriteCond
# We need this line because /page.php?i=page.php will cause an infinite loop
RewriteCond %{QUERY_STRING} !i=page.php
# Now we parse out the value of i from the query string
RewriteCond %{QUERY_STRING} i=([^&]+)
RewriteRule ^page.php /%1 [L]

Related

Changing URLs when using $_get to determine webpage

I currently use $_GET['base'] to determine which homepage that the user visits.
This results in localhost/?base=administrator or localhost/?base=guest
I am also using this to control which page is the user at, such as
localhost/?base=guest&page=register
Is there any way to use mod_rewrite, or htaccess, to change how this system works?
Modifying my code is not an issue, is this possible?
EDIT:
I am trying to achive this:
localhost/?base=guest to localhost/guest
localhost/?base=admin to localhost/admin
localhost/?base=guest&page=register to localhost/guest/register
Below is my htaccess file
RewriteEngine On
RewriteRule ^([^/]*)/([^/]*)$ /?base=$1&page=$2 [L]
RewriteRule ^([^/]*)$ /?base=$1 [L]
Will the document path affect how it is being called? As I am using a case loop to include which items are needed.
This, however, works for localhost, but it will loop every other address to main.
RewriteEngine On
RewriteRule ^$ /index.php?base=guest[L]
But did not give a result as expected.
Your rules in .htaccess need to be in reverse order, like below:
RewriteRule ^([^/]*)/([^/]*)$ /?base=$1&page=$2 [L]
RewriteRule ^([^/]*)$ /?base=$1 [L]
That is because if it is kept in the order you have it, both localhost/?base=guest&page=register & localhost/?base=administrator will match the rule RewriteRule ^([^/]*)$ /?base=$1.
Having them in reverse order ensures that the first rule is matched only for localhost/?base=guest&page=register. It won't match the first rule for localhost/?base=administrator. I hope that helps.
You need to exclude your existent files and folders from the rule
RewriteEngine On
# if the request is a dir
RewriteCond %{REQUEST_FILENAME} -d [OR]
# or file
RewriteCond %{REQUEST_FILENAME} -f
#do nothing
RewriteRule ^ - [L]
RewriteRule ^([^/]*)/([^/]*)$ /?base=$1&page=$2 [L]
RewriteRule ^([^/]*)$ /?base=$1 [L]
So you can use this simple code:
RewriteEngine on
RewriteRule ^(\w+)$ index.php?base=$1 [L]
RewriteRule ^(\w+)/(\w+)$ index.php?base=$1&page=$2 [L]
\w will match symbols a-z, 0-9 and underscore _, I think those characters are enough for your case, but if you need expansion it will be easy
Also in this case you don't need to change your code, because you still get base and page parameters in the $_GET array
UPDATE:
to disable query string params page and base (other params may be needed) add these two lines to the code at the bottom:
RewriteCond %{THE_REQUEST} (\?|&)(page|base) [NC]
RewriteRule .* - [L,R=404]

Remove Trailing Slash from Specific URL using HTACCESS

How to force remove trailing slash from specific URL using htaccess,
For example :
https://blahblah.com/checkout/?checkout=add_to_cart&id=17&item_options[price_id]=1/
to
https://blahblah.com/checkout/?checkout=add_to_cart&id=17&item_options[price_id]=1
and ignore other URL such as https://blahblah.com/about/ or https://blahblah.com/contact/ etc
Because the trailing slash is part of the querystring you can't just rewrite it, you have to extract the querystring minus that final / and then redirect to the page you're on with that match appended.
To do this you need to match on the pattern in the RewriteCond with %1 (see this answer for reference) and append that to the %{REQUEST_URI} (thus removing the original querystring) - like this:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^(.*)/$
RewriteRule ^.*$ %{REQUEST_URI}?%1 [L,R=301]
RewriteCond %{QUERY_STRING} ^(.*)/$ <-- make sure you've got that / at the end here in your conditional - it ensures that only querystrings that end with / are redirected so you don't end up in a horrible recursive loop.
You can try this:
RewriteCond %{QUERY_STRING} (.*)/$
RewriteRule ^(.*)$ /$1?%1 [L,R=301]
This will redirect every request containing query strings only. In your case this:
checkout/?checkout=add_to_cart&id=17&item_options[price_id]=1/
to
checkout/?checkout=add_to_cart&id=17&item_options[price_id]=1
But not this https://blahblah.com/about/ and https://blahblah.com/contact/
I would advise trying to find out why that trailing slash is there to begin with first. As CD001 said in the comments it could be that your PHP code is doing something wrong.
If however you do need a htaccess solution to remove the slash then the below should work for you.
RewriteCond %{QUERY_STRING} ^(.*)/$
RewriteRule ^(.*)$ ?%1 [L,R=301]
This has been tested at http://htaccess.madewithlove.be/ and works for your example URLs.
if you mean an internal routine
RewriteCond %{QUERY_STRING} ^(checkout\=[^\&\s]+\&id\=\d+\&item_options\[price_id\]\=\d+)\/
RewriteRule ^ %{REQUEST_FILENAME}?%1 [L]

mod_rewrite: rewrite existing files to friendly links

I went through a bunch of websites and tutorials yet can't find a solution.
Following snippet works and http://example.com/page/pot return a pot.php content
RewriteEngine on
RewriteRule ^page/([^/]*)$ $1.php?page=$1 [L]
I can't get it to work the other way around
RewriteEngine on
RewriteRule ^([^/]*)/page$ $1.php?page=$1 [L]
Your current approach will cause infinite looping since Apache re-injects rewritten URI back for further rule processing.
You need to use THE_REQUEST variable for that like this:
RewriteEngine On
# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+page/[^.]+\.php\?page=([^\s&]+) [NC]
RewriteRule ^ page/%1? [R=302,L]
# internal forward from pretty URL to actual one
RewriteRule ^page/([^/]*)/?$ $1.php?page=$1 [L,QSA,NC]
Try to use:
RewriteEngine on
RewriteRule ^([^/]+)/page$ $1.php?page=$1 [L]

Mod rewrite only when directory exists

I have a directory structure as follows:
/gallery
----index.php
----/23XASDTAGH
----/24XGA43KJA/
I'd like to use mod rewrite to rewrite if a directory exists. so:
www.example.com/gallery/23XASDTAGH/
becomes
www.example.com/gallery/index.php?gallery=23XASDTAGH
but i'd like to do this silently, so no changes happen to the url. now i have this:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME}/ -d
RewriteRule (.*) /gallery/index.php?gallery=$1 [L]
which works with
www.example.com/gallery/23XASDTAGH/
but the odd thing is when i leave the trailing slash off the end it changes the url to
www.example.com/gallery/23XASDTAGH/?gallery=23XASDTAGH
how can i get it to work with or without the trailing slash?
I think the following will do the trick
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule (.*) /gallery/index.php?gallery=$1 [L]
Second line checks that the requested filename is a directory and if it is then the third line does the actual rewrite
To try and deal with the trailing slash problem, maybe this, or something like this will work
RewriteCond %{REQUEST_FILENAME} (.*)/?$
RewriteCond %1 -d
RewriteRule (.*)/$ /gallery/index.php?gallery=$1 [R=301]
You may want to try this:
RewriteEngine on
RewriteRule ^(gallery)/([a-zA-Z0-9-_=]+)/?$ http://www.example.com/$1/index.php?$1=$2 [R=301,L]
Permanent redirect the entered URL (The one in the browser address bar):
www.example.com/gallery/23XASDTAGH or www.example.com/gallery/23XASDTAGH/to:
www.example.com/gallery/index.php?gallery=23XASDTAGH
Finally figured it out, i needed a ?/ at the end of my expression in the rewrite rule to say that the slash was optional. Why do regular expressions have to be so hard easy. 8-)
correct expression:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^([a-zA-Z0-9_-]+)?/$ /gallery/index.php?gallery=$1 [L]

How can I transform a query string generated by a search form into new URL without the query string?

I have a search form that on submit generates something like this url:
http://mydomain.com/index.php?find=some+text
And I'm aiming to make it look like:
http://mydomain.com/find/some+text
How can I do this with .htaccess ?
So far I have this:
RewriteCond %{REQUEST_URI} ^/index\.php$
RewriteCond %{QUERY_STRING} &?find=(.*)&?
RewriteRule ^index.php$ find/%1? [R=301,L]
RewriteRule ^find/(.*)$ index.php?find=$1 [L]
This works if the query (i.e what I search for) contains only numbers, letters or underscore, but I want to make it capable of search for anything including spaces and other characters!
So, it seams that the 404 error was being cause because of some configuration in the web server that did not permit URLs to have + (plus sign).
It would break at the first + and try to find a file by that name.
Having that sorted out the rewrite rules were like this:
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /*.index\.php\?find=([^&\.]+)?\sHTTP
RewriteRule ^/?index.php$ /find/%1? [L,R=301]
RewriteRule ^/?find/(.*)$ /index.php?find=$1 [L,NC]
Thanks for your help!
Shouldn't something like this work?
RewriteRule ^find/([^/]*)$ /index.php?find=$1 [L]
You need to match against the actual request and not the URI because the URI gets rewritten via your second rule. This causes a redirect loop.
To redirect the browser:
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.php\?find=([^&\.]+)(&([^\ ]+))?
RewriteRule ^/?index.php$ /find/%1?%3 [L,R=301]
To internally rewrite it back
RewriteRule ^/?find/(.*)$ /index.php?find=$1 [L,QSA]

Categories