I have the following .htaccess file:
RewriteEngine On
RewriteBase /
RewriteRule ^/(.*)/(.*)/(.*)$ index.php?pageLevel1=$1&pageLevel2=$2&pageLevel3=$3 [L,R=301]
When I access my website using http://www.domain.com/testpage/ it gives me the 404 Not Found error. What am I doing wrong?
PS: currently the index.php files just echo the pageLevel1, pageLevel2 and pageLevel3 values.
Remove the leading slash from Rewrite pattern
RewriteEngine On
RewriteBase /
RewriteRule ^(.*)/(.*)/(.*)$ index.php?pageLevel1=$1&pageLevel2=$2&pageLevel3=$3 [L,R=301]
First of, I don't know why are you using the 301 redirection at all? If you want to accept all characters in the url you just need to have a catch-all rewrite rule like this:
RewriteRule ^(.*)$ index.php?param=$1 [L]
If you want to catch the parameters like you defined, the rewrite rules can be like this:
RewriteRule ^([A-Za-z-]+)/([A-Za-z-]+)/([A-Za-z-]+)$ index.php?pageLevel1=$1&pageLevel2=$2&pageLevel3=$3 [L]
UPDATE:
The completed rules in your case (three page levels), with URL slugs which accepts letters and numbers can be like:
RewriteRule ^([A-Za-z0-9-]+)$ index.php?pageLevel1=$1&pageLevel2=$2 [L]
RewriteRule ^([A-Za-z0-9-]+)/([A-Za-z0-9-]+)$ index.php?pageLevel1=$1&pageLevel2=$2 [L]
RewriteRule ^([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/([A-Za-z0-9-]+)$ index.php?pageLevel1=$1&pageLevel2=$2&pageLevel3=$3 [L]
This way you can access pages like:
www.domain.com/test
www.domain.com/test/another-test
www.domain.com/test/another-test/new-level
Related
I want to create pretty url. But, I got some problem with .htaccess. For example I have url domain/some.php?f=query-string.
I want to change domain/query-string (expected url). Is that possible to change / redirect via .htaccess. Or maybe from php file itsself.
this is a bit of htaccess snippet i made, but i get it blank/error page
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]
RewriteRule ^/([^/.]+)$ some.php?f=$1 [NC,L]
</IfModule>
Thanks for your attention.
RewriteRule ^/([^/.]+)$ some.php?f=$1 [NC,L]
In .htaccess, the URL-path matched by the RewriteRule pattern does not start with a slash, so the above will never match and it will do nothing. This should be written like the following instead:
RewriteRule ^([^/.]+)$ some.php?f=$1 [L]
The NC flag is not required here, since the regex is already "case-insensitive".
hi i have use many different htaccess codes, but i cant get rewrite to work.
i want this url
https://domain.com/category.php?cat=firm
to look like this url
https://domain.com/category/firm
this is my latest attempt
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^([^/]*)\.html$ /category.php?cat=$1 [L]
ErrorDocument 404 https://seoboost.no/404page.php
RewriteCond %{HTTP_HOST} ^www\.seoboost\.no$
RewriteRule ^/?$ "https\:\/\/seoboost\.no\/" [R=301]
RewriteCond %{HTTP_HOST} ^www\.example\.com$
RewriteRule ^/?$ "https\:\/\/example\.com\/" [R=301,L]
RewriteCond %{HTTP_USER_AGENT} libwww-perl.*
RewriteRule .* ? [F]
RewriteRule ^index\.php$ / [R=301]
RewriteRule ^(.*)/index\.php$ /$1/ [R=301]
i have try to delete all in my htaccess and only have this code
RewriteEngine on
RewriteRule ^([^/]*)\.html$ /category.php?cat=$1 [L]
but it still not working, is my server or what am i doing wrong??
Try replacing your .htaccess with
RewriteEngine On
RewriteRule ^category/(.*)$ /category.php?cat=$1 [L]
which will redirect yoursite.com/category/12 to yoursite.com/category.php?cat=12 internally, and the user will never see the "ugly" url. Then inside your category.php file you can access the cat by $category_id = $_GET['cat']
A simple anchor tag looks like this:
Item 12
Pulling from Justin Lurman's answer here, the following should work:
RewriteEngine On
# redirect "/category.php?cat=..." to "/category/..." to hide a directly accessed "ugly" url
RewriteCond %{THE_REQUEST} \s/category\.php\?cat=(.+)\s [NC]
RewriteRule ^ /category/%1? [R=301,L]
# internally rewrite "/category/..." to "/category.php?cat=..."
RewriteRule ^category/(.+)$ /category.php?id=$1 [L]
As Justin noted in that answer to a similar question, you need to confirm that htaccess files are enabled/allowed in your Apache configuration and that mod_rewrite is enabled.
Additionally, I am sure you are aware, but just in case you are not, after implementing this new htaccess redirect rule, you should change all of the links on your webpages to use clean/friendly URLs. By doing so, neither your users nor a search engine crawler will access the "ugly" URLs unless they access them directly (via a bookmark or a saved link).
I have following rewriterules:
RewriteEngine On
RewriteRule ^([^\-]*)-(.*)-von-(.*)\.html$ $1index.php?filter=$2&marke=$3 [QSA]
RewriteRule ^([^\-]*)-von-(.*)\.html$ $1index.php?marke=$2 [QSA]
RewriteRule ^([^\-]*)-(.*)\.html$ $1index.php?filter=$2 [QSA]
RewriteRule ^(.*)\.html$ $1index.php
The first 3 rules are working, but the fourth, just rewrite index.php to .html is not. What is wrong here?
EDIT:
The URL is example.com/folder/subfolder/index.php
In the folder I got following htaccess:
RewriteEngine on
RewriteRule ^subfolder(.*) subfolder/$1
And the htaccess in subfolder is the one above
Now the URL for the first rule is example.com/folder/subfolder-value1-von-value2.html and works, for the second and third rule it's example.com/folder/subfolder-value1.html and example.com/folder/subfolder-von-value2.html
So with logic the fourth rule should also work just without the parameters but it's not working
Place this in /folder/.htaccess:
RewriteEngine on
RewriteRule ^subfolder\.html$ subfolder/index.php [L,NC]
RewriteRule ^subfolder(.+) subfolder/$1 [L,NC]
You shouldn't add $1 to the second part of the rule.it'll append the first matching group to the index.php file name. what you are currently getting is 'indexindex.php'
if you just want to rewrite index.html to index.php then you can place following line at the end of the file.
RewriteRule ^index.html$ index.php [L,NC]
also you might wanna remove $1 part from other lines as well.
Can you try this & see ?
RewriteEngine on
RewriteRule ^(.*)\.html $1\.php
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]
I need a rewrite rule to rewrite the following:
www.abc.com/page/xyz to www.abc.com/xyz
remove **"page"** from url
I think it's more like this:
RewriteBase /
RewriteRule ^(.*)$ page/$1 [L]
If I understand what you're trying to do.
Like so:
RewriteBase /
RewriteRule ^page(/.*)?$ $1 [L]
If you only want to redirect page/xyz then you can use:
RewriteEngine on
RewriteRule ^/page/xyz$ /xyz [L]
If you want to redirect anything within the page directory, then use:
RewriteEngine on
RewriteRule ^/page(/.*$)$ $1 [L]