I have such urls:
http://site.ru/ontent/wefwefw/article.php?article=369-tayskaya-kuhnya-recept-s-foto
http://site.ru/ontent/wefwefw/article.php?article=32237-ogurci-recepti-na-zimu
http://site.ru/ontent/wefwefw/article.php?article=90-ogurci-na-zimu-recepti-po-koreyski
I want to rewrite tham like:
http://site.ru/tayskaya-kuhnya-recept-s-foto.html
http://site.ru/ogurci-recepti-na-zimu.html
I tried smth like:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ ontent/wefwefw/$1 [L]
But how cut unnecessary parts of string?
Unless article.php is able to derive the right article from the title alone, what you want to do is not possible. mod_rewrite is good at rewriting things, but it can't summon an article-id from thin air if it isn't in the original request. You would have something like:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)\.html$ ontent/wefwefw/article.php?article=$1 [L]
When you would request http://site.ru/tayskaya-kuhnya-recept-s-foto.html, it will load http://site.ru/ontent/wefwefw/article.php?article=tayskaya-kuhnya-recept-s-foto. Then you have to get the id 369 in some other way based on the title if needed.
The best in this case is:
http://site.ru/369-tayskaya-kuhnya-recept-s-foto.html
redirect to
http://site.ru/ontent/wefwefw/article.php?article=369
with:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(\d+)-[^/]*\.html$ ontent/wefwefw/article.php?article=$1 [L]
Related
My URL looks like this:
example.com/index.php?f=directory&s=page
I want it to be like this instead:
example.com/directory/page
my rewrite rules look like this right now:
RewriteRule ^([A-Za-z0-9]+).html$ https://www.example.net/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^/?(.*?)/?$ index.php?s=$1 [L]
it appears to work but not quite because if I call the queries in PHP I get something like this:
print $_GET['f'] */ it prints nothing
print $_GET['s'] */ it prints directory/page
which is not what I intend. How can I fix it?
Thank you.
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^/?(.*?)/?$ index.php?s=$1 [L]
Do it like this instead:
RewriteRule ^([^/]+)/([^/]+)$ index.php?f=$1&s=$2 [L]
Request /directory/page and it internally rewrites the request to index.php?f=directory&s=page.
By making the regex more specific, the filesystem checks can probably be avoided.
To redirect from index.php?f=directory&s=page to /directory/page (if these URLs have already been indexed and/or linked to by third parties), then add the following redirect before the above directive:
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{QUERY_STRING} ^f=([^&]+)&s=([^&]+)$
RewriteRule ^index\.php$ /%1/%2 [R=301,L]
But you must already be linking to the canonical /directory/page URL in your application.
Test with 302s to avoid potential caching issues. Clear your browser cache before testing.
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^/?(.*?)/?$ index.php?s=$1 [L]
You are only assigning a single URL parameter s, so yes, $_GET['f'] will indeed be empty.
I have been trying to force remove some unnecessary part of my url for example remove index.php? from this url.
/index.php?task=boost,boosting&action=index
The following rules apply not helped
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?/$1 [L]
how should look like the correct rule to hide unnecessary parts such as index.php or task or action?
With below rule you can use url yourdomain.com/boost/index you have to still pass value to task & action in some form so you cannot hide all the string from url until it is a post request.
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([\w-]+)/([\w-]+)$ index.php?task=$1&action=$2 [QSA,NC,L]
Description You may use the following
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>
I need to simultaneously do two things with htaccess.
I need to take a URL like:
http://client.example.com/123
and rewrite the directory to a param, and simultaneously add another subdomain to the url so it looks like this:
http://client.qa.example.com/?param=123
This does the param bit correctly, but I can't figure out how to add the subdir:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^/[^/]+/?$
RewriteRule ^([^/]*)/?$ /?param=$1 [L]
You can examine the host header using a RewriteCond and extract the relevant parts of the name. Use them in the rewrite. Back references to matches in RewriteConds appear as %n
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{HTTP_HOST} (.+?)\.(.*)
RewriteRule ^([^/]*)/?$ http://%1.qa.%2/?param=$1 [R,L]
(.+?)\.(.*) will do a match on everything up to the first . and then everything to the end. So client and example.com will respectively be in %1 and %2
If your .htaccess is in the root of client.example.com, it should be a simple redirect. Of course the directory has to be a fake directory or this won't redirect.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/?$ http://client.qa.example.com/?param=$1 [R=301,QSA,L]
You can use the following to match (check for htaccess syntax):
(http://[^.]+\.)([^/]+/)([^/]*)/?$
And replace with:
$1qa.$2?param=$3
See DEMO
Finally got it working using:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{HTTP_HOST} (.+?)\.(.*)
RewriteRule ^([^/]*)/?$ http://%1.qa.%2/?param=$1 [R,L]
Now I just have to figure out how to work in 2 parameters, given that param 2 isn't always going to be present.
My original url was this:
wwww.domain.com/car-details.php?merk=BMW&model=X5&titel=sale&car_id=3
I have Rewrite it into this:
wwww.domain.com/BMW/X5/sale/3
By putting this in my .htaccess:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/(.*)/(.*)/([0-9]+)?$ /car-details.php?merk=$1&model=$2&titel=$3&car_id=$4 [L,QSA]
When i put in Url wwww.domain.com/car-details.php?merk=BMW&model=X5&titel=sale&car_id=3 it still works. I want that url to be redirected to wwww.domain.com/BMW/X5/sale/3
I have tried to put the [R] flag in my htaccess RewriteRule but then I'm getting the opposite results.
How can I fix this?
This is a good decision to do what you want.
But you will face a loop problem.
You can avoid it by using this code
RewriteEngine on
RewriteCond %{THE_REQUEST} \s/car-details\.php\?merk=([^&]+)&model=([^&]+)&titel=([^&]+)&car_id=([0-9]+) [NC]
RewriteRule . /%1/%2/%3/%4? [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/([0-9]+)?$ /car-details.php?merk=$1&model=$2&titel=$3&car_id=$4 [L]
I am working on a CMS that requires an id and a title to be sent to the page content.php so that the data can be fetched from the database. I have tried to use:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([0-9]+)/(.*) content.php?id=$1&title=$2
But when I navigate to /id, it doesn't work, nor does it work if i do /id/title. What have I messed up here? Anything to remember for future use in .htaccess?
Thanks in advance!
You may try this:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} /([^/]+)/([^/]+)
RewriteRule .* http://example.com/content.php?key1=%1&key2=%2 [L]
It will map silently this:
http://example.com/id/title
To this:
http://example.com/content.php?key1=id&key2=title
Where /id in the incoming URL is the value of id key and the same for /title