I'm trying to make SEO Friendly url by altering php parameters with slash based url.
I want to make
mysite.com/TopList.php?tl=ToplistName&fr=2021-02-10&to=2021-02-20
into:
mysite.com/ToplistName/2021-02-20/2021-02-20
I had success with rewriting url but none of my includes are referring to right directory path and I get no css, js and file paths are broken from links.
This is what I have in .htaccess file now:
RewriteRule ^([^/\.]+)/([^/\.]+)/([^/\.]+)?$ TopList.php?tl=$1&fr=$2&to=$3
Anyone that can help me get this sorted out inside .htaccess file?
Based on your shown samples, could you please try following.
Please make sure you clear your browser cache before testing your URLs.
RewriteEngine ON
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)/(\d{4}-\d{2}-\d{2})/(\d{4}-\d{2}-\d{2})/?$ TopList.php?tl=$1&fr=$2&to=$3 [L]
I solved this by doing following:
added to html:
<base href="http://example.com/">
added to .htaccess:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
#1 Changing Full query
RewriteRule ^([^/.]+)/([^/.]+)/([^/.]+)/?$ TopList.php?tl=$1&fr=$2&to=$3
#2 Changing only first landing page
RewriteRule ^([^/.]+)/?$ TopList.php?tl=$1
this gave me following result:
mysite.com/TopList.php?tl=ToplistName ==> mysite.com/ToplistName
mysite.com/TopList.php?tl=ToplistName&fr=2021-02-10&to=2021-02-20 ==> mysite.com/ToplistName/2021-02-10/2021-02-20
Related
OK so I've found every rendition of this but not this specifically. So my urls are in this format
www.inspection.com/users/?action=register
I want to convert it to
www.inspection.com/users.php?action=register
Here's what I've got so far in my htaccess
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^[A-Za-z0-9]+/\?action=[A-Za-z0-9]+$ $1.php?action=$2 [L,QSA]
But error logs say RewriteRule: cannot compile regular expression
Main Goal: After the domain name is the reference to the file I want to call. So append php on the end of that string but keep the query parameters
With your shown samples, attempts please try following .htaccess rules file. Please make sure to clear your browser cache before testing your URLs. Also make sure that your .htaccess rules file and .php file are residing on same folder.
RewriteEngine ON
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/?]*)/?\?action=(.+)$ $1.php?action=$2 [NC,L,QSA]
One more thing, you have not created a capturing group on left side of RewriteRule hence you can't use them on the right side of it.
I have a URL called
http://localhost:8080/text/index.php?id=2
and I have to redirect on
http://localhost:8080/text/2
So I added the below code in the .htaccess but it's not working
RewriteEngine On
RewriteRule ^([^/d]+)/?$ index.php?id=$1 [QSA]
I refer to the below two links. Is there any issue with my code?
common-htaccess-redirects-19-6-2018 and htaccess-rules
After suggested answer, I tried below code
HTML
Register
login
or
Register
login
.htaccess code
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^[\w-]+/(\d+)/?$ index.php?id=$1 [QSA,L]
With your shown samples, please try following. Please make sure to clear your browser cache before testing your URLs. This considers that you are
hitting URL http://localhost:8080/text/2 in browser.
##Making RewriteEngine ON here.
RewriteEngine ON
##Placing conditions to check if these are non-existing pages only.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
##Writing rule to rewrite to index.php with needed variable here.
RewriteRule ^[\w-]+/(\d+)/?$ index.php?id=$1 [QSA,L]
Issues in OP's attempt: You are trying to attempt to match digits in starting where your url doesn't have digits, so rather use [\w-]+ with it. Also use QSA,L flags with your rewrite rule to handle query string and come out of the rule in case this is executing.
Is there a way to change URL by htaccess ?
ex: current url is www.abc.com/about want change to www/abc.com/test/about
It's not only "about" link but also all links want to add "/test/" before the link
Yes it is possible to achieve that effect using Apache URL Rewriting. Just make the following changes to your .htaccess
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^test/([a-zA-Z0-9-]*)$ /$1 [NC]
Explanation
The first line of code does as it says, it turns Apache’s Rewrite Engine on. The following 3 lines checks if the requested URL is already a valid file, it only redirects if it isn’t. Meaning , if your website already contains a directory like /test/mydir, then the redirection will not occur. The final line rewrites the URL giving us the desired output.
I've just added a simple rewrite rule to my .htaccess file to drop .php from this page http://themeat.in/register.php
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/$ $1.php
RewriteRule ^([^/]+)/([^/]+)/$ /$1/$2.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule (.*)$ /$1/ [R=301,L]
but now when I go visit that page without the .php (http://themeat.in/register/) all my styles and files have vanished. When I open up the console I see the page name is being treated as a folder.
This is what the file path should be and was before the rewrite, http://themeat.in/css/styles.css
and this is what it is now,
http://themeat.in/register/css/styles.css
I guess it's got something to do with the trailing slash within the rewrite but I'm totally stumped at how to fix this problem? I need the .php dropped and I'd like to keep the trailing slash.
Any help would be greatly appreciated.
Many thanks,
//C
This is because of the rewritten urls. when the url is example.com/register/ apache thinks /register/ is a directory and appends that in front of all relative urls.
To solve this, You can add the following base tag in head section of your webpage :
<base href="/">
For more info, see this post : Seo Friendly Url css img js not working
I used this code in my .htaccess to rewrite my urls which is like this:
www.example.com/subcategory.php?subcat=my-test
to
www.example.com/my-test
.htaccess code I used:
RewriteEngine on
RewriteCond $1 !^(subcategory\.php|resources|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ subcategory.php?subcat=$1 [L,QSA]
My problem is whenever I access the url not pertaining to my subcategory.php file like contactus and aboutus, the .htaccess file will somehow put me to subcategory.php file which is not what I want. I want my contactus be handled by contactus.php and aboutus with aboutus.php.
I know that there is something wrong with my .htaccess file but I couldn't fix it by myself for I am not so familiar with the .htaccess coding.
Any suggestion would be greatly appreciated.
Thank you very much!
You are rewriting all requests to subcategory.php. There is no way for .htaccess to tell whether /xxx is a subcategory or some different page, so you could redirect it to a different script.
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php [L]
Use this code snippet instead of yours and move all the "routing" logic to PHP. In PHP, you can find out whether /xxx is a subcategory, a contact page, an article or something else and use a script suitable for that kind of database record.
You will find the requested URL in $_SERVER['REQUEST_URI'].