I've been trying to get a better URL format than the regular one. I have to deal with parameters, and I'd like to get from :
http://www.whatever.com/embed.php?site=site1&id=videoid
to :
http://www.whatever.com/embed/site/site1/id/videoid
I've been trying to get something like this using .htaccess, but I still don't understand how it really works.
This is what I have for now :
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
RewriteRule ^embed/(.*)/(.*)$ /embed.php?site=$1&id=$2
The three first lines actually hide the .php extension, which is okay, but the next one doesn't work as I wish it would !
Any one as an idea ?
Thanks in advance guys !
Try it,
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^\.]+)$ $1.php [NC,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^embed/site/([\w-]+)/id/([\w-]+)$ embed.php?site=$1&id=$2 [QSA,L]
Related
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]
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]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . Core.php
Now I have this above but, would like to know how/if I can mix these into one.
I'm new to this .htaccess so if you could give me a link or somewhere to learn that would be great. ;)
I also couldn't find an awnser that worked on google searches or anything explain how it works so I can configure it to work for me. :L
You can use :
#Rewrite /file to /file.php
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^ %{REQUEST_URI}.php [NC,L]
# Rewrite any other request to /core.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ /core.php [NC,L]
Explaination :
RewriteCond %{REQUEST_FILENAME}.php -f
The line above checks to ensure that the URI.php is a file, if it is an existent php file, then
RewriteRule ^ %{REQUEST_URI}.php [NC,L]
The rule rewrites the request to its orignal location request.php (file => file.php)
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]
I am having a big time issue in using htaccess but got a little more familiar with the help of this forum. but there is one issue where I cant seem to solve with the search function of this forum or perhaps my key words are an issue of its own. here goes the scenario where I hope you guys would enlighten me on:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{QUERY_STRING} ^u=([0-9])$
RewriteRule ^([0-9])/([^/]*)$ /example/index.php?u=$1&m=$2 [QSA,NC,L]
the above modrewrite is suppose to help me to detect if the parameter for "u" exist and its in numerical form, it will rewrite to the above rules. If else it would use the below rewrite rules.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{QUERY_STRING} ^!u=([0-9])$
RewriteRule ^([^/]*)$ /example/index.php?m=$1 [QSA,NC,L]
Well, as you can see, its not working very well. I really do appreciate any help given here. thanks in advance.
I just found a solution to my own question. Ironic as that is. Here you go:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([0-9]+)/?$ example/index.php?u=$1 [QSA,NC,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)/?$ example/index.php?m=$1 [QSA,NC,L]
It's for those who wants to know. =)