Rewrite multiple url's with htaccess - .htaccess

I'm struggling with this problem a few weeks now. In Google Search Console I get many crawl errors with the same problem: Google cannot find url's that don't even exist.
I've looked in the html-code, but the relative url's are all fine. And I'm using the /-base for all my internal links. I think the problem is my .htaccess file.
On my website nationsleaguevoetbal.nl I have two url's with different rewrites:
/nieuws/item
/wedstrijd/id/land
'land' isn't used and is only for looking nice. Now Google Search Console can't find for example:
/wedstrijd/id/nieuws/item
It combines the two url's where it shouldn't.
My .htaccess rewrite looks like:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)$ /index.php?pagina=$1 [QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^nieuws/([^/]+)$ /index.php?pagina=nieuws&item=$1 [QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^wedstrijd/([^/]+)/([^/]+)$ /index.php pagina=wedstrijd&id=$1&landen=$2 [QSA,L]
I thought the QSA would solve the problem, but the errors are coming back. Can you help me please?

Have it this way:
RewriteEngine On
# skip all files and directories from rewrites
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteRule ^nieuws/([^/]+)/?$ index.php?pagina=nieuws&item=$1 [QSA,L,NC]
RewriteRule ^wedstrijd/([^/]+)/([^/]+)/?$ index.php?pagina=wedstrijd&id=$1&landen=$2 [QSA,L,NC]
RewriteRule ^(.+?)/?$ index.php?pagina=$1 [QSA,L]

Related

".htaccess" file not working at all

I have this code in my htaccess file
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME}\.php !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [NC]
This help to remove the .php extension in my url. but now I've been trying for weeks to rename a URL I have.
article.php?num=5 to article/num/5 or maybe the title of the article it self nothing has worked so far. this is what I tried using.
RewriteRule ^article/([0-9].+)/?$ article?num=$1 [NC]
Yet I always get internal server error i really need help with this its been frustrating.
Your RewritePattern doesnt accept the /num/ segment, you need to adjust it so it matches your your uri /article/num/digits . Change your pattern's regex to this
^article/num/([0-9]+)$
Try it like below rule,
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^([\w-]+)$ $1.php [NC,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([\w-]+)/num/([\d]+)$ $1.php?num=$1 [QSA,L]

Creating frendly url

I'm struggling with it all day. I try to create friendly urls to my localhost. My site link is for example www.example.com and when i for example want see news i want to link from www.example.com/news.php look like www.example.com/news and i did this with this code
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
And everything is worked but i want to also my posts and users and other dont look like www.example.com/users/?id=1 or for posts www.example.com/posts/?id=3231 i want to have like this www.example.com/users/1 or www.example.com/posts/3231 and at the same time my php files dont have .php extensions. So basicly i want www.example.com/somepage.php to www.example.com/somepage and www.example.com/somepage.php?somevalue=something to www.example.com/somepage/something
You can try this in your .htaccess in the root.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(users|posts)/([^/]+)/?$ /$1/?id=$2 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)/?$ $1.php?somevalue=$2 [NC,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/?$ $1.php [NC,L]

Need to rewrite parameters AND redirect with htaccess

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.

.htaccess Issue: JS and CSS Rending index.php

I'm using a WAMP installation and routing everything through my index.php file. The CSS and JS files in my header get called just fine when I remove the .htaccess file, so the file paths are correct, but once I insert the .htaccess file again Chrometools throws this error upon including my JS file
Uncaught SyntaxError: Unexpected token <
When viewing Chrometool's Network>>Response, it shows the JS file contains the content of everything rendered by the index.php file.
Here's my code
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} \.(gif|jpe?g|png|js|css|swf|php|ico|txt|pdf|xml)$ [OR]
RewriteRule ^([^/]*)$ index.php?_page=$1
RewriteRule ^([^/]*)/([^/]*)$ index.php?_page=$1&_action=$2 [QSA,L]
RewriteRule ^([^/]*)/([^/?]*)/(.*)$ index.php?_page=$1&_action=$2&$3 [QSA,L]
I thought the %{REQUEST_FILENAME} !-f was supposed to prevent any rewriting of a file when it's called directly?
UPDATE
I've found the line causing the issue is is the second RewriteRule.
RewriteRule ^([^/]*)/([^/]*)$ index.php?_page=$1&_action=$2 [QSA,L]
Because my JS and CSS are stored two directories deep from my project root, the rule is directing the file to my index.php.
Neither of these below commands have any affect until I comment out the RewriteRule
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !\.(gif|jpe?g|png|js|css|swf|php|ico|txt|pdf|xml)$ [NC]
Any ideas?
Try and change this line.
RewriteCond %{REQUEST_FILENAME} \.(gif|jpe?g|png|js|css|swf|php|ico|txt|pdf|xml)$ [OR]
To this
RewriteCond %{REQUEST_FILENAME} !\.(gif|jpe?g|png|js|css|swf|php|ico|txt|pdf|xml)$ [NC]
See of that works for you.
Edit:
You need the conditions for all your rules. Rewrite Conditions only applies to the first rule following it. Also you can just have it so if it matches static content, then just not do anything. Try your rules this way.
RewriteCond %{REQUEST_FILENAME} \.(gif|jpe?g|png|js|css|swf|php|ico|txt|pdf|xml)$ [NC]
RewriteRule ^(.*)$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)$ index.php?_page=$1
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)/([^/]*)$ index.php?_page=$1&_action=$2 [QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)/([^/?]*)/(.*)$ index.php?_page=$1&_action=$2&$3 [QSA,L]

Redirect url get variables after rewriting htaccess

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]

Resources