My current .htacess looks like this:
RewriteEngine On
RewriteRule ^([a-zA-Z0-9-z\-]+)$ index.php?page=$1
RewriteRule ^([a-zA-Z0-9-z\-]+)/$ index.php?page=$1
which allows me to take a a link such as
http://www/myserver.com/somepage
and rewrite it into
http://www.myserver.com/?page=somepage.
but how would i make it go deeper? I want to be able to do:
http://www.myserver.com/somepage/subpage
and turn it into
http://www.myserver.com/?page=somepage&subpage=subpage
Thanks
Add the following rules with additional capturing group for the second parameter
RewriteRule ^([a-zA-Z0-9-z\-]+)/([a-zA-Z0-9-z\-]+)$ index.php?page=$1&subpage=$2
RewriteRule ^([a-zA-Z0-9-z\-]+)/([a-zA-Z0-9-z\-]+)/$ index.php?page=$1&subpage=$2
If you have the potential to catch links that do not have subpages, then you'd want to catch the subpages with the first rule. Then you can catch the rest and send to page.
RewriteEngine On
# catch potential subpages first
RewriteRule ^([a-zA-Z0-9-z\-]+)/([a-zA-Z0-9-z\-]+)/?$ index.php?page=$1&subpage=$2 [L]
#combines your original rule to allow for optional end slash
RewriteRule ^([a-zA-Z0-9-z\-]+)/?$ index.php?page=$1 [L]
You could combine them into one rule, but it would potentially generate queries with an empty subpage. I prefer the cleaner internal queries, but for arguments sake:
RewriteEngine On
# One rule to rule them all
RewriteRule ^([a-zA-Z0-9-z\-]+)(/([a-zA-Z0-9-z\-]+))?/?$ index.php?page=$1&subpage=$3 [L]
Edit: Throwing in this link to a rewrite tester. Very useful. Although, when I tested my rules, it brought up an issue with your original [] grouping. What is the -z- at the end for?
RewriteEngine On
RewriteRule ^([a-zA-Z0-9-z\-\_]+)$ index.php?page=$1
RewriteRule ^([a-zA-Z0-9-z\-\_]+)/$ index.php?page=$1
RewriteRule ^([a-zA-Z0-9-z\-\_]+)/([a-zA-Z0-9-z\-\_]+)$ index.php?page=$1&id=$2
RewriteRule ^([a-zA-Z0-9-z\-\_]+)/([a-zA-Z0-9-z\-\_]+)/$ index.php?page=$1&id=$2
RewriteRule ^([a-zA-Z0-9-z\-\_]+)/([a-zA-Z0-9-z\-\_]+)/([a-zA-Z0-9-z\-\_]+)$ index.php?page=$1&id=$2&value=$3
RewriteRule ^([a-zA-Z0-9-z\-\_]+)/([a-zA-Z0-9-z\-\_]+)/([a-zA-Z0-9-z\-\_]+)/$ index.php?page=$1&id=$2&value=$3
Related
I have a problem with my .htaccess, a short explanation I would like to set http://example.com/newest on my website. However, it always redirects to http://example.com/postname. Where I just need the exact "newest" page. Here is my code:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteBase /
RewriteRule ^[^/]+$ %{REQUEST_URI}/ [L,R=301]
RewriteRule ^/category/(.*)$ page.php?f=$1
RewriteRule ^/search/(.*)$ search.php?f=$1
RewriteRule ^(.*)/$ post.php?f=$1 <- If this is removed, my post htaccess will not work
RewriteRule ^newest/$ index.php?f=newest <- I want to execute this code
I really don't know what this is called, I have been looking for the whole stackoverflow but I did not get any answer. Please remain me if this is a duplicate question.
As Mohammed implied in comments, your directives are in the wrong order. The line above your "newest" rewrite is a catch-all and rewrites all requests, so the last line will never match.
http://example.com/newest
Note that your rules imply that your URLs should end in a trailing slash. So, you should be linking to http://example.com/newest/ (with a trailing slash), not http://example.com/newest, otherwise your users will get a lot of unnecessary redirects.
However, you appear to be under the belief that the RewriteCond directive applies to all the directives that follow. This is not the case. It only applies to the first RewriteCond directive. You also need some L flags to prevent further processing.
You also have a slash prefix on the "category" and "search" rewrite patterns, so these would never match in a .htaccess context.
Try something like the following instead:
RewriteEngine On
RewriteBase /
# Don't process the request further if it maps to an existing file
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]
# Append trailing if omitted
# Although strictly speaking this only redirects if there are no slashes at all in the URL
RewriteRule ^[^/]+$ %{REQUEST_URI}/ [L,R=301]
RewriteRule ^category/(.*)$ page.php?f=$1 [L]
RewriteRule ^search/(.*)$ search.php?f=$1 [L]
RewriteRule ^newest/$ index.php?f=newest [L]
RewriteRule ^(.*)/$ post.php?f=$1 [L]
I'm trying to create clean urls using .htaccess on my website i have this
RewriteEngine on
RewriteRule ^([^/]*)/$ /full_posts.php?permalink=$1 [L]
RewriteRule ^category/([^/]*)/$ /search.php?category=$1 [L]
the first one is working correctly but the second one is not working, I'm writing both one after another. please help!
The rules you are looking for are:
RewriteRule ^([^/]*)/?$ /full_posts.php?permalink=$1 [L]
RewriteRule ^category/([^/]*)/?$ /search.php?category=$1 [L]
I have made the trailing slash optoinal in both the cases.
You can test your rules on:
http://htaccess.madewithlove.be/
On my blog (which I'm in the process of redesigning for a more efficient way of displaying content), I have a RewriteRule setup to handle a simple redirection from
http://new.kn3rdmeister.com/blog.php
to
http://new.kn3rdmeister.com/category/blog/ (and it works backwards too)
In the .htaccess:
RewriteEngine On
RewriteRule ^blog\.php$ /category/blog/ [R=301,L]
RewriteRule ^category/blog/?$ blog.php [L]
...
That works just dandy. Now what I would like to do is rewrite
/blog.php?pagenum=x
to
/category/blog/page/x/
In the .htaccess:
...
RewriteCond %{THE_REQUEST} ^(GET|POST|HEAD)\ blog\.php\?pagenum=([0-9]*)\ HTTP/
RewriteRule ^blog\.php$ /category/blog/page/%2/ [R=301,L]
RewriteRule ^category/blog/page/([0-9]+)/$ blog.php [L]
All I can get to work is the first rule, which ends up working, but it displays the QueryStrings rather than the oh-so-clean page directories, like so...
/blog.php?pagenum=2 for example equals
/category/blog/?pagenum=2
I hope I've made my question clear enough :/ If you need anything clarified, please ask and I'll update my post. Thanks in advance.
Add a question mark to the end of your target in your RewriteRule:
RewriteRule ^blog\.php$ /category/blog/page/%2/? [R=301,L]
I've changed my URL structure to remove the category from the URLs of item pages.
I need some way of redirecting all the old pages to the new pages, using mod_rewrite. I have already a quite elaborate and messy .htaccess file.
What I'd need is to redirect:
http://example.com/category-name/item-name/ to this http://example.com/download-item-name/
Right now, I've got this in the htaccess file.
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} !^example.com$ [NC]
RewriteRule ^(.*)$ http://example.com/$1 [R=301]
RewriteRule ^download-([^/]*)/screenshots/$ /screenshots.php?screenshots=$1 [L]
RewriteRule ^download-([^/]*)/screenshots/([^/]*)/$ /screenshots.php?screenshots=$1&shot=$2
#and now without trailing slash, so request will hit script which can do redirect.
RewriteRule ^download-([^/]*)/screenshots/([^/]*)$ /screenshots.php?screenshots=$1&shot=$2 [L]
RewriteRule ^feed/([^/]*)/$ /feed/?category=$1 [L]
RewriteRule ^download-([^/]*)-software/$ /category.php?category=$1 [L]
#this needs to stay above the below rule
RewriteRule ^download-([^/]*)/$ /software.php?shortname=$1 [QSA]
RewriteRule ^download-([^/]*)/([^/]*)/$ /software.php?shortname=$1&lang=$2 [QSA]
#and now without trailing slash, so request will hit script which can do redirect.
RewriteRule ^download-([^/]*)$ /software.php?shortname=$1 [QSA]
RewriteRule ^download-([^/]*)/([^/]*)$ /software.php?shortname=$1&lang=$2
RewriteRule ^downloading/([^/]*)/$ /download.php?downloading=$1
RewriteRule ^downloading/([^/]*)/([^/]*)/$ /download.php?downloading=$1&lang=$2 [L]
RewriteRule ^download/([^/]*)/([^/]*)/$ /send.php?key=$1&s=$2 [QSA]
RewriteRule ^FQ-([^/]*)/$ /content.php?page=$1 [L]
RewriteCond %{REQUEST_URI} ^(/[^/]+)(/.*)?$
RewriteCond %{DOCUMENT_ROOT}/categories%1/ -d
RewriteRule . /categories%{REQUEST_URI} [QSA]
Options -Indexes
FileETag None
ErrorDocument 404 http://example.com/404/
ErrorDocument 403 http://example.com/
#Some redirects
Redirect 301 /audio-video-players-codecs/zoom-player-max/ http://example.com/download-zoom-player-max/
Redirect 301 /audio-video-players-codecs/zoom-player-premium/ http://example.com/download-zoom-player-premium/
Redirect 301 /audio-video-players-codecs/zoom-player-pro/ http://example.com/download-zoom-player-pro/
Your .htaccess is messy because of simple things:
Rule #1: avoid mixing Redirect and RewriteRules directives. This will only confuse things. There's always a possibility to replace Redirect by RewriteRules directives.
Rule #2: if you don't need RewriteBase / (which is often the case, because the base is often /), don't write it. This will only confuse things. The shorter the better (that's what she tells me (just kiddin')).
Rule #3:: ^(.*)$ is the same as (.*). Here too, this makes things clearer.
Rule #4:: special rule for you: you can reduce by 2 almost all your RewriteRules using a thing like ^downloading/([^/]*)(/([^/]*))?/ which makes the second argument empty but match both one or two arguments (just valid in your Php that you can get an empty second argument). The shorter the better.
Then to answer your question: to redirect: http://example.com/category-name/item-name/ to this http://example.com/download-item-name/
RewriteRule ^category-([a-zA-Z]+)/item-([a-zA-Z]+)/ ^download-item-$2/ [QSA,R]
This should work
My favorite tool to check for regexp:
http://www.quanetic.com/Regex (don't forget to choose ereg(POSIX) instead of preg(PCRE)!)
I have entries in my .htaccess that goes like this:
RewriteRule ^$ dubai/ [R]
RewriteRule ^dubai/$ page/index.php [L]
RewriteRule ^$ pakistan/ [R]
RewriteRule ^pakistan/$ page/index.php [L]
basically I want to make a rule that can handle countries as part of the rewrite, the above code i have is redundant and can cause the file to grow unnecessarily.
Is there a way to automatically redirect the page to page/index.php if it serves for any countries? I tried putting this line to the last line above:
RewriteRule ^([a-zA-Z\/]*)/$ page/index.php
My problem is it no longer reads the succeeding rules for the other pages to serve. The below lines are samples of the rules just below the last one above:
RewriteRule ^([a-zA-Z\/]*)rent/$ page/index.php?methodcall=rent&for=Rent
RewriteRule ^([a-zA-Z\/]*)landlords/$ page/index.php?methodcall=landlords
What happens is the get vars are no longer read by index.php file, if i remove the last rewriterule the key vars are read properly.
Any suggestions?
Thanks in advance!
Try this:
RewriteEngine on
RewriteRule ^([a-z]+)(\/?)$ page/index.php [NC,QSA,L]
RewriteRule ^([a-z]+)/rent(\/?)$ page/index.php?methodcall=rent&for=Rent [NC,QSA,L]
RewriteRule ^([a-z]+)/landlords(\/?)$ page/index.php?methodcall=landlords [NC,QSA,L]