This permalink/friendly url:
http://anything.com/ok
Should be serving this path/url:
http://anything.com/path/to/page/dir1/dir2/page.php?parameter=ok
This works, but this is hardcoded. need dynamic method. dir1 can be anything, dir2 can be anything, and the text after parameter= can be anything.
RewriteRule ^ok/{0,1}$ path/to/page/dir1/dir2/page.php?parameter=ok [QSA,L]
Here are bunch of things i tried without success.
#RewriteRule ^page/([a-zA-Z0-9_-]+)$ page.php?parameter=$1
#RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /page\.php\?parameter=([^\&\ ]+)
#RewriteRule ^/?page\.php$ /page/%1? [L,R=301]
#RewriteCond %{REQUEST_FILENAME} !-f
#RewriteCond %{REQUEST_FILENAME} !-d
#RewriteRule ^path/to/page/([^/]+)/([^/]+)$ page.php?parameter=$1 [L]
#RewriteRule ^page/{0,1}$ http://%4/path/to/page/%3/%2/page.php?parameter=%1 [QSA,L]
#RewriteRule ^$4/{0,1} http://$1/path/to/page/$2/$3/page.php?parameter=$4 [QSA,L]
#RewriteRule ^page/{0,1}$ http://%1/path/to/page/%2/%3/page.php?parameter=%4 [QSA,L]
#RewriteRule ^page/{0,1}$ http://$1/path/to/page/$2/$3/page.php?parameter=$4 [QSA,L]
#RewriteRule ^$ http://$1/path/to/page/$2/$3/page.php?parameter=$4 [QSA,L]
#RewriteRule ^$4 http://$1/path/to/page/$2/$3/page.php?parameter=$4 [QSA,L]
#RewriteRule ^$4 http://%1/path/to/page/%2/%3/page.php?parameter=%4 [QSA,L]
#RewriteRule ^/{0,1}$ http://$1/path/to/page/$2/$3/page.php?parameter=$4 [QSA,L]
#RewriteRule ^/{0,1}$ http://%1/path/to/page/%2/%3/page.php?parameter=%4 [QSA,L]
#RewriteRule ^/{0,1}$4 http://%1/path/to/page/%2/%3/page.php?parameter=%4 [QSA,L]
#RewriteRule ^/$4 http://%1/path/to/page/%2/%3/page.php?parameter=%4 [QSA,L]
#RewriteRule ^/$4 http://$1/path/to/page/$2/$3/page.php?parameter=$4 [QSA,L]
Edit:
This worked for a random parameter. Now I just need to know how to do random two folders in the middle of url.
RewriteRule ^([^/.]+)/?$ path/to/page/dir1/dir2/page.php?parameter=$1 [L]
Examples of random 2 folders:
/admin/11-blah/
/development/546-whatever/
/abc-company/46208-name-of-project/
Could be anything as those 2 directory names. No special characters except dash though. alphanumeric and dash.
Edit 2:
Am i to understand that the friendly rewrite has to contain all the dynamic elements of the full dirty url?
I got this to work:
RewriteRule ^([^/.]+)/([a-zA-Z0-9-]+)/([a-zA-Z0-9-]+)/?$ path/to/page/$1/$2/page.php?parameter=$3 [L]
which means the clean url will look like this:
http://anything.com/admin/11-blah/ok
but there's no way to make it look like this?:
http://anything.com/ok
Related
My .htaccess file:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [NC,L]
RewriteRule ^([a-zA-Z0-9-]+)$ index.php?page=$1 [QSA,L]
RewriteRule ^([a-zA-Z0-9-]+)/$ index.php?page=$1 [QSA,L]
I have defined one key in the URL i.e. "page", which is working fine. Now I need to include a 2nd key but the "key" will be different for every page.
For example:
example.com/index.php?page=user?id=john
example.com/index.php?page=product?url=product-url
need to convert them to:
example.com/user/john
example.com/product/product-url
How can I define individual second keys based on page?
You can use:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [NC,L]
RewriteRule ^user/([^/]+)/?$ index.php?page=user&id=$1 [NC,QSA,L]
RewriteRule ^product/([^/]+)/?$ index.php?page=product&url=$1 [NC,QSA,L]
RewriteRule ^([a-zA-Z0-9-]+)/?$ index.php?page=$1 [QSA,L]
I know there's a bajillian number of questions with this title and I've tried them all. What I'm trying to do is redirect
localhost/site/tours/picnics/
to
localhost/site/tours/picnics
but every code I've tried, (this one for instance)
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{THE_REQUEST} \s(.+?)/+[?\s]
RewriteRule ^(.+?)/$ /$1 [R=301,L]
redirects me to localhost/picnics for some reason. I don't understand why it's happening. I've even tried RewriteBase /site/ but it didn't make any difference. Can someone point out the problem in this code?
Edit: Here's my complete htaccess file
RewriteEngine On
#RewriteBase /site/
RewriteRule ^destinations/(.*)$ destinations.php?destId=$1 [NC,L]
#RewriteRule ^tours/? tour-listing.php [NC,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{THE_REQUEST} \s(.+?)/+[?\s]
RewriteRule ^(.+?)/$ /$1 [R=301,L]
RewriteRule ^tours/?(.*)$ tour-listing.php?cat=$1 [NC,L]
RewriteRule ^tour/([0-9a-zA-Z]+)$ tour.php?id=$1 [NC,L]
RewriteRule ^hotel/([0-9a-zA-Z]+)$ hotel-details.php?id=$1 [NC,L]
Change your rule to this:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{THE_REQUEST} \s/+(.+?)/+[?\s]
RewriteRule /$ /%1 [R=301,L,NE]
$1 captures value relative to your current directory only but %1 is being captured from %{THE_REQUEST} that has original and full REQUEST URL.
I have this snippet in my .htaccess file:
RewriteCond %{QUERY_STRING} id=([0-9]+)
RewriteRule ^(20\d\d)/page/([0-9]+)/$ ?yearMeasure=$1&page=$2 [L]
RewriteRule ^(20\d\d)/$ ?yearMeasure=$1 [L]
RewriteRule ^category/([A-Za-z0-9-]+)/page/([0-9]+)/$ ?category=$1&page=$2 [L]
RewriteRule ^category/([A-Za-z0-9-]+)/$ ?category=$1 [L]
RewriteRule ^(20\d\d)/([A-Za-z0-9-]+)/$ ?type=post&year=$1&title=$2 [L,QSA]
However, when I remove the first line, the website still behaves in the same way. The URLs still remain "clean." This brings me to my question; what is the significance of RewriteCond in this snippet?
The only RewriteRule this RewriteCond affected was this:
RewriteRule ^page/([0-9]+)/$ ?page=$1 [L]
...when I attempted to visit /page/2/, it didn't work.
When I moved it above the RewriteCond as follows:
RewriteRule ^page/([0-9]+)/$ ?page=$1 [L]
RewriteCond %{QUERY_STRING} id=([0-9]+)
RewriteRule ^(20\d\d)/page/([0-9]+)/$ ?yearMeasure=$1&page=$2 [L]
RewriteRule ^(20\d\d)/$ ?yearMeasure=$1 [L]
RewriteRule ^category/([A-Za-z0-9-]+)/page/([0-9]+)/$ ?category=$1&page=$2 [L]
RewriteRule ^category/([A-Za-z0-9-]+)/$ ?category=$1 [L]
RewriteRule ^(20\d\d)/([A-Za-z0-9-]+)/$ ?type=post&year=$1&title=$2 [L,QSA]
...everything came back to normal. Why?
In the docs RewriteRule is defined this way:
RewriteRule Pattern Substitution [flags]
If Pattern matches the url requested (domain.com/path/file.cgi), the url is replaced with the Substitution (either a redirect header is returned or the url is changed internally depending on the flags)
This rewrite rule:
RewriteRule ^(20\d\d)/page/([0-9]+)/$ ?yearMeasure=$1&page=$2 [L]
will check if the url matches the pattern and the replacement will occur only in this case. That's why here such RewriteCond is useless:
RewriteCond %{QUERY_STRING} id=([0-9]+)
It's useful in situations that RewriteRule can't capture like checking the hostname or whether a file exists:
RewriteCond %{HTTP_HOST} !^example.com$ [NC]
RewriteRule .? http://example.com%{REQUEST_URI} [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
http://www.sitepoint.com/apache-mod_rewrite-examples-2/
I'm stucked in a little problem:
I want to make a short link to let the user view the images of the user "test.test".
My code:
RewriteRule ^(.*)\.(.*)$/images images.php?user-id=$1.$2 [QSA,L]
Example:
website.com/tester.tester/images <== This has to be the end-result
Full Code here:
RewriteEngine On # Turn on the rewriting engine
RewriteRule ^(login|settings|friends|gallery|logout|signup|administrator)/?$ $1.php [NC,L]
RewriteRule ^home/?$ profile.php?user-id=home [QSA,NC,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond $1 !^index [NC]
RewriteRule ^(.*)\.(.*)$ profile.php?user-id=$1.$2 [QSA,L]
# RewriteRule ^([A-Za-z0-9-\s]+)\.([A-Za-z0-9-\s]+)$ profile.php?user-id=$1.$2 [QSA,L] #Umlaute müssen gehen!!
Well I tried my best and hope it will work. But I'm not happy about that:
The Updated Code:
RewriteEngine On # Turn on the rewriting engine
RewriteRule ^(login|settings|friends|gallery|logout|signup|administrator)/?$ $1.php [NC,L]
RewriteRule ^home/?$ profile.php?user-id=home [QSA,NC,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{THE_REQUEST} ^(.*)\.(.*)\/images [NC]
RewriteRule ^(.*)\.(.*)\/images$ images.php?user-id=$1.$2 [QSA,L]
RewriteCond %{THE_REQUEST} ^(.*)\.(.*)\/images\/ [NC]
RewriteRule ^(.*)\.(.*)\/images\/$ images.php?user-id=$1.$2 [QSA,L]
RewriteCond %{THE_REQUEST} !^(.*)\.(.*)\/images\/ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond $1 !^index [NC]
RewriteCond %{THE_REQUEST} ^(.*)\.(.*) [NC]
RewriteRule ^(.*)\.(.*)$ profile.php?user-id=$1.$2 [QSA,L]
RewriteCond %{THE_REQUEST} ^(.*)\.(.*)\/ [NC]
RewriteRule ^(.*)\.(.*)\/$ profile.php?user-id=$1.$2 [QSA,L]
The Result is correct, but if the user types in "tester.tester/" wrong page is loaded.
This is annoying me. BUT I tried it and even this little thing might be solved by PHP (I don't really know)
Thanks for all Answers!! :D
You closed the expression prematurely with $, which would cause an error.
This should work for you:
RewriteRule ^(.*)\.(.*)/images$ images.php?user-id=$1.$2 [QSA,L]
Your file should now look like this:
RewriteEngine on
RewriteRule ^(login|settings|friends|gallery|logout|signup|administrator)/?$ $1.php [NC,L]
RewriteRule ^home/?$ profile.php?user-id=home [QSA,NC,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond $1 !^index [NC]
RewriteRule ^(.*)\.(.*)\/images$ images.php?user-id=$1.$2 [QSA,L]
RewriteRule ^(.*)\.(.*)$ profile.php?user-id=$1.$2 [QSA,L]
# RewriteRule ^([A-Za-z0-9-\s]+)\.([A-Za-z0-9-\s]+)$ profile.php?user-id=$1.$2 [QSA,L] #Umlaute müssen gehen!!
I am trying to do something very simple yet still confusing.
I have a link
http://sub.example.com/folder/?qvar=value
I want this to be accessible via:
http://sub.example.com/folder/value
Very simple yet I cannot figure this out.
I have tried the following to no avail:
#RewriteEngine On
#RewriteRule ^(.*)$ ?qvar=$1 [L, QSA]
#RewriteRule ^(.*)$ /folder/index.php?qvar=$1 [L,QSA]
#RewriteRule ^(.*)$ index.php?qvar=$1 [L,QSA]
#RewriteRule ^(.*)$ /folder/?qvar=$1 [L,QSA]
#RewriteRule ([^/\.]+)/?$ index.php?qvar=$1 [L,QSA]
#RewriteCond %{QUERY_STRING} qvar=(.*)
#RewriteRule index.php %1 [L]
Some of these give me a 500 internal server error.
Others redirect me to /sub/sub/folder/.
Please help!
Try:
Options +FollowSymLinks -Multiviews
RewriteEngine On
RewriteCond %{HTTP_HOST} ^sub\.example\.com$ [NC]
RewriteCond %{THE_REQUEST} \ /folder/\?qvar=([^&\ ]+)
RewriteRule ^ /folder/%1? [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{HTTP_HOST} ^sub\.example\.com$ [NC]
RewriteRule ^folder/(.*)$ /folder/?qvar=$1 [L,QSA]
These rules need to be in the htaccess file in your document root