Having trouble figuring out the mod rewrite for .htaccess I want the url http://www.example.com/archive.php?title=about_me which is a dynamic url to be rewritten to http://www.example.com/about_me. I am using php and here is my current .htaccess code, however it only rewrites to http://www.example.com/archive/about_me want the archive to be removed.
Options +FollowSymLinks -MultiViews
rewriteengine on
RewriteBase /
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/archive\.php\?title=([^&\ ]+)
RewriteRule ^ /archive/%1? [L,R=301]
RewriteRule ^/?archive/(.*)$ /archive?title=$1 [L]
## Hide .php extension
## To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,L,NC]
I did get it to rewrite correctly with this code
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/archive\.php\?title=([^&\ ]+)
RewriteRule ^ /%1? [L,R=301]
RewriteRule ^/(.*)$ /archive?title=$1 [L]
However it then returns a page cannot be found error
I you want the /archive/ to be removed, you'll have to ensure that any URI that's in the form of /something must absolutely be routed to the archive.php script. Because there's simply no way to tell whether /my_blog is actually a request for /my_blog or whether it needs to be sent to the archive.php script with "my_blog" as the value of title in the query string. The best you can do is check that it's not a request for an existing resource via the -f and -d conditions:
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/archive\.php\?title=([^&\ ]+)
RewriteRule ^ /%1? [L,R=301]
# no /archive/ ^
# condition checks
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/?(.*)$ /archive?title=$1 [L]
Something like this should do the trick:
RewriteCond %{QUERY_STRING} ^title=(.*)
RewriteRule ^archive.php /%1?
RewriteRule ^(.*)$ /archive?title=$1 [L]
EDIT: Added the final RewriteRule as noted in my comments on the original question. Per the comment I believe you are trying to do the following two things:
Redirect any user-entered "real" URLs to the "friendly" URL: http://www.example.com/archive.php?title=about_me to http://www.example.com/about_me as stated in the question.
Rewrite the "friendly" URL to the "real" URL: http://www.example.com/about_me to http://www.example.com/archive.php?title=about_me, which was not clear as stated.
Related
I'm trying to shorten my urls in the address bar and for Google indexation purposes. For example, a real server directory path
http://www.somewebsite.com/path1/path2/path3/
would display
http://www.somewebsite.com/path3/
I've found many similar topics but no answer that seems to work for this particular case.
I have for example:
RewriteRule ^path3(.*)$ path1/path2/path3$1 (tried with with [L], [QSA,l], [R=301,...]...)
But this simply does a redirect and does not keep the short address in the browser.
My .htaccess file looks as follow:
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
# Use UTF-8 encoding for anything served as `text/html` or `text/plain`.
AddCharset UTF-8 .html
# Force www.
RewriteCond %{HTTP_HOST} !=""
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
# remove .php; use THE_REQUEST to prevent infinite loops
RewriteCond %{THE_REQUEST} ^GET\ (.*)\.php\ HTTP
RewriteRule (.*)\.php$ $1 [R=301]
# remove index
RewriteRule (.*)/index$ $1/ [R=301]
# remove slash if not directory
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} /$
RewriteRule (.*)/ $1 [R=301]
# add .php to access file, but don't redirect
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteCond %{REQUEST_URI} !/$
RewriteRule (.*) $1\.php [L]
#Trying to have a shorter url in address bar
RewriteRule ^path3(.*)$ path1/path2/path3$1
Converting my comments to answer so that solution can be easily found for the problem stated.
There are couple of issues with the rules shown in question:
Since target paths are pointing to an existing directory and you don't have trailing / in target it is causing an external redirect by mod_dir module of Apache that appends a / at the end of directory path and performs a 301 redirect.
Incorrect positioning of rule.
Not critical but missing L and NE (no escape) flag from redirect rules which may cause problems for some cases.
With those suggestion final working .htaccess can be like this:
# Use UTF-8 encoding for anything served as `text/html` or `text/plain`.
AddCharset UTF-8 .html
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
# Force www.
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L,NE]
# remove .php and index; use THE_REQUEST to prevent infinite loops
RewriteCond %{THE_REQUEST} \s/+(.*?/)?(?:index|(\S+?))\.php[/\s?] [NC]
RewriteRule ^ /%1%2 [R=301,L,NE]
# remove slash if not directory
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} /$
RewriteRule (.*)/$ $1 [R=301,L,NE]
# rewrite path3/ to /path1/path2/path3/
RewriteRule ^path3/.*$ path1/path2/$0 [L,NC]
# add .php to access file, but don't redirect
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteCond %{REQUEST_URI} !/$
RewriteRule ^(.*?)/?$ $1.php [L]
The example you provided works for myself on my own test website, using the below .htaccess rewrite:
RewriteEngine On
RewriteRule ^test2.txt/?$ /test.txt
This lets me have an optional trailing slash, and show the content of test.txt on /test2.txt.
Assuming your rewrite engine is on, can you replicate this behaviour? What version of Apache are you using? Is the path handled by a CMS at all?
I've read and followed guides and looked for answers to other people's questions, but I'm struggling with url rewriting and htaccess.
I have a subfolder on my site with an index page which handles query strings to bring up dynamic content. I'd like the variable to appear to be a subfolder name e.g.
http://mywebsite.com/subfolder/index.php?v=variable
to
http://mywebsite.com/subfolder/variable
The .htaccess file I've made is at http://mywebsite.com/subfolder/ i.e. the same folder as index.php
How can I get this to work? Any help gratefully appreciated.
You can use these rules inside your /subfolder/.htaccess
RewriteEngine On
RewriteBase /subfolder/
RewriteCond %{THE_REQUEST} \s/subfolder/index\.php\?v=([^&\s]+) [NC]
RewriteRule ^ %1? [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)$ index.php?v=$1 [L]
Update (passing two values)
RewriteEngine On
RewriteBase /subfolder/
RewriteCond %{THE_REQUEST} \s/subfolder/index\.php\?v=([^&\s]+)&v2=([^&\s]+)\s [NC]
RewriteRule ^ %1/%2? [R=301,L]
RewriteCond %{THE_REQUEST} \s/subfolder/index\.php\?v=([^&\s]+)\s [NC]
RewriteRule ^ %1? [R=301,L]
# Don't touch to existing files/folders
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]
# Rewrite /xxx to /index.php?v=xxx
RewriteRule ^([^/]+)$ index.php?v=$1 [L]
# Rewrite /xxx/yyy to /index.php?v=xxx&v2=yyy
RewriteRule ^([^/]+)/([^/]+)$ index.php?v=$1&v2=$2 [L]
Use this in your .htaccess:
RewriteEngine On
RewriteCond %{QUERY_STRING} v=(.+)$ [NC]
RewriteRule ^ subfolder/%1? [R=301,L,NE]
This grabs the variable using %{QUERY_STRING} and then appends it to the rewrite using %1. You'll see I've added a ? onto the end of the rewrite. That is to stop the original query from appearing on the end of the URL.
I've used R=301 which is a permanent redirect. You might want to changes this to R=302 while you're testing, as this is temporary.
You can view a test of this rule working here: https://htaccess.madewithlove.be?share=7b6832e9-2c05-5d1d-916c-e4dd0f5b1da6
Make sure you clear your cache before testing this.
I have such rewrite rules
RewriteEngine On
RewriteCond %{HTTP_HOST} ^monkey.pl(.*) [NC]
RewriteRule ^(.*)$ http://www.monkey.pl/$1 [R=301,L]
RewriteRule ^horse.html$ /dog.html
and when I go to the monkey.pl/horse.html I get the message:
The requested URL /home/login/monkey/dog.html was not found on this server.
How can I get this to work. Basically what I'm trying to do is to change address of urls like:
http://www.monkey.pl/produkty.php?strona=1
to be displayed as
http://www.monkey.pl/produkty/czesci_do_mixokretow.html
but none of my rules are working. Therefore I'm trying to come with solution.
I tried many varations and I couldn't get it to work. I don't want to rewrite whole page. Just 6 pages which I need to change url and that's all. Fixed translation url => url.
If you are only doing a handful of URLs then you can do them this way.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteCond %{THE_REQUEST} /+produkty\.php\?strona=1 [NC]
RewriteRule ^ /produkty/czesci_do_mixokretow\.html [R=302,L]
RewriteRule ^produkty/czesci_do_mixokretow\.html$ /produkty.php?strona=1 [L]
RewriteRule ^horse\.html$ /dog.html [L]
I need to remove the /design/ from this URL (and any that contain it).
https://www.domain.com/design/subcategory/productname1
At the end, I want it to look like this:
https://www.domain.com/subcategory/productname1
But since that would obvious cause a 404 of you just deleted that part of the URL, I need to populate that second URL with the contents of the first. I apologize if I am not using the correct terms but here is an example of something similar I have on my site.
In my header menu, I have a link that indicates it will take you here:
https://www.domain.com/index.php?route=product/search&tag=shirts
But I wanted it to look like this:
https://www.domain.com/shirts/
So I researched it and this solution worked great:
# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} \s/+product/search/\?tag=([^\s&]+) [NC]
RewriteRule ^ /%1/? [R=301,L,NE]
# internal forward from pretty URL to actual one
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/?$ product/search/?tag=$1 [L,QSA]
Can something similar be done to remove the /design/ part of the URL?
Thanks!
Try these rules:
RewriteEngine On
# remove /design/ from URLs and redirect
RewriteCond %{THE_REQUEST} \s/+design/(\S*)\s [NC]
RewriteRule ^ /%1 [R=301,L,NE]
# internally add design/ to know set of URIs
RewriteRule ^(skulls-bones|characters|abstract|animals|games|geek-nerd|graphic|keep-calm|movies-tv|pop-culture|tattoo|typography)(/.*)?$ design/$1$2 [L,NC]
# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} \s/+product/search/\?tag=([^\s&]+) [NC]
RewriteRule ^ /%1/? [R=301,L,NE]
# internal forward from pretty URL to actual one
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !^/design/ [NC]
RewriteRule ^([^/]+)/?$ product/search/?tag=$1 [L,QSA]
I have a question about htaccess.
I have a url like this :
http://www.asd.com/producten/kia?category=car
And I want this to be :
http://www.asd.com/producten/car/kia
Is that possible with htaccess?
I have tried different ways to solve it but so far no luck.
I've tried:
RewriteRule ^producten/([^/]+)/([^/]+) /producten/$2?category=$1 [NC]
AND
RewriteRule /producten/(.*)/(.*) /producten/$2?category=$1 [R]
Thanks in advance,
Mert
Try using the following in your /.htaccess file:
RewriteEngine On
# Step 1: Redirect the old URI to the new one and prevent looping
RewriteCond %{ENV:REDIRECT_STATUS} !200
RewriteCond %{QUERY_STRING} category=([^\s&]+) [NC]
RewriteRule ^(producten)/([^/]+)/? /$1/%1/$2? [R=302,L,NE]
# Step 2: Internally rewrite the new URI to the old one
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(producten)/([^/]+)/([^/]+)/?$ /$1/$3?category=$2 [L,QSA]
If you would like to make the redirect permanent, change R=302 to R=301.
You can use this code in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} /(producten)/([^?]+)\?category=([^\s&]+) [NC]
RewriteRule ^ /%1/%3/%2? [R=302,L,NE]
# internal forward from pretty URL to actual one
RewriteRule ^(producten)/([^/]+)/([^/]+)/?$ $1/$3?category=$2 [L,QSA,NC]