Make SEO Friendly URL in Php with help of .htaccess - .htaccess

I want to make my URL from this:
https://www.website.com/index.php?title=A1-13-05-2021
to this:
https://www.website.com/A1-13-05-2021
And want to access above query string param in index.php as $_GET['title'].
For this, I have written the below .htaccess code. But that's not working
RewriteEngine On
RewriteRule ^index?$ index.php
RewriteRule ^index/([a-zA-Z0-9\-]+) index.php?title=$1
Can anyone please help me to know where I am doing wrong?
Thanks.

With your shown samples, could you please try following. Please make sure to clear your browser cache before testing your URLs.
RewriteEngine ON
##Rule for handling non-existing pages here to index.php file.
RewriteRule ^(A1-13-05-2021)/?$ index.php?title=$1 [QSA,NC,L]
Generic Rules: In case you want to make it Generic rules then try following rules only.
RewriteEngine ON
##Rule for handling non-existing pages here to index.php file.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(A1-\d{2}-\d{2}-\d{4})/?$ index.php?title=$1 [QSA,NC,L]

Related

PHP Dynamic URL Parameters and GET

I'd like to ask if it's possible to use a dynamic url and GET at the same time.
Let's say my current dynamic url is: https://yourdomain.com/blog/this-is-a-title
Would it be possible to make this work too: https://yourdomain.com/blog/this-is-a-title?action=delete
RewriteEngine on
RewriteRule ^([0-9]+)$ index.php?id=$1
The dynamic url mentioned first works fine, but I want to make the second work as well.
This is my .htaccess - hope it helps.
PS: I know that the regex in my htaccess isn't correct, it's just an example.
Have your .htaccess Rules file in following manner. Please make sure that your htaccess Rules file is present in root folder(where blog and htaccess both are residing in it; htaccess shiouldn't be inside blog folder; should be place same folder with it). Make sure to clear your browser cache before testing your URLs.
RewriteEngine ON
##Newly added rules here...
RewriteBase /blog/
RewriteCond %{THE_REQUEST} \s/blog/(?:[^?]*)?action=(\S+)\s [NC]
RewriteRule ^ index.php?action=%1 [L]
##Old Rules OP's htaccess ones.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([0-9]+)$ index.php?id=$1

Url rewriting but keeping PHP backend the same?

Let's say I have this URL:
http://mydomain.com/app/editor/?id=59500
I would like to have this url to instead show the following in the browser address bar:
http://mydomain.com/app/editor/59500
...but have the PHP page ("http://mydomain.com/app/editor/index.php") remain the same. In otherwords, I still want to have this page to be able to execute $_GET['id']; and return "59500".
Would I use regex in HTACCESS for this? Any advice on the best approach and an example would be greatly appreciated.
You want to use a .htaccess rewrite for this. You can make the redirect happen only when the URL ends with a number (to avoid redirecting index.php).
The \d+ gets all digits, and the /? will allow URLS that either have a slash after the ID or not.
Something like:
<ifmodule mod_rewrite.c>
RewriteRule ^app/editor/(\d+)(/?)$ app/editor/index.php?id=$1
</ifmodule>
Try adding these rules to the htaccess file in the /app/editor/ directory:
RewriteEngine On
RewriteBase /app/editor/
RewriteCond %{THE_REQUEST} \ /+app/editor/(index\.php)?\?id=([0-9]+)
RewriteRule ^ %1? [L,R]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([0-9]+)/?$ index\.php?id=$1 [L]

Want to rewrite URL through HTACCESS

I'm sure this has been asked in other forms, but I can't for the likes of me get this thing working after googling/trying for hours.
I have a bunch of URLs like this:
www.yoursite.com/pic_box.php?pic=$
What I want is that all URLs will only be avaiable with clean URLs.
www.yoursite.com/your-title-here
Can any htaccess master help me with this?
In your .htaccess:
RewriteEngine on
RewriteRule ^picbox/(.*)$ /pic_box.php?pic=$1 [NC,L]
Will give you the ability to rename your url like that:
www.yoursite.com/picbox/your-pic-id
Try adding these rules to the htaccess file in your document root:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /pic_box.php?pic=$1 [L]

htaccess mod_rewrite exclude images/css

I have created a simple mod rewrite for my site:
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/(cgi-bin|css|images|js|gfx)/?.*$ [NC]
RewriteRule ^filter/model/([^/]*)/mins/([^/]*)/texts/([^/]*)/freegifts/([^/]*)/network/([^/]*)/merchant/([^/]*)$ /fiverrr.php?model=$1&mins=$2&texts=$3&freegifts=$4&network=$5&merchant=$6 [L]
but it doesn't seem to be working (as in all the images/js etc don't show up)
e.g. http://mydomain.com/filter/model/Sony+Ericsson+Xperia+Arc+S+White/mins/+3000/texts/+-1/freegifts/FREE+Nintendo+3DS+Black/network/Orange/merchant/
original url: http://mydomain.com/fiverrr.php?model=Sony+Ericsson+Xperia+Arc+S+White&mins=+3000&texts=+-1&freegifts=FREE+Nintendo+3DS+Black&network=Orange&merchant=Contract
Where am I going wrong, and is this the best method?
Thanks so much
Try to put there another condition - htaccess will rewrite it only if requested file does not exist:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^filter/model/([^/]*)/mins/([^/]*)/texts/([^/]*)/freegifts/([^/]*)/network/([^/]*)/merchant/([^/]*)$ /fiverrr.php?model=$1&mins=$2&texts=$3&freegifts=$4&network=$5&merchant=$6 [L]
Also I recomend you to check full paths. Check if you have images in HTML code like: http://mydomain.com/images/img.jpg - relative paths like ./../images/img.jpg will not work!

Rewriting URL using .htaccess and mod_rewrite

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'].

Resources