Make Pretty Url's for Custom CMS with .htaccess - .htaccess

I am making my own CMS for my social networking website. I'm facing a problem, I need to clean the url to make it pretty like facebook does.
URL Rewrite using HTACCESS
Urgly URL => www.domain.com/index.php?options=profile&id=393
Clean URL => www.domain.com/393
Currently am using this code below to remove inde.php?options from url
RewriteCond %{REQUEST_FILENAME} !index.php
RewriteRule .* index.php?options=$0 [QSA,L]
I have searched Google and tired everything but it does not work for me. Something is weird...
please help am stuck really badly..!

I suppose your rule+condition will match every request including images, scripts, stylesheets etc and will redirect it to index.php. I doubt that's what you want.
You can rather do it the other way around. If people visit your index.php or top domain name (eg. www.domain.com) do nothing.
RewriteRule ^index\.php$ - [L]
Then you can rewrite all applicable requests (paths that doesn't exist) to index.php.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* /index.php?options=$0 [L,QSA]
Add these rules+conditions to your .htaccess and see if it meets your requirements.

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

How to stop custom url being redirected to homepage?

Question - I have custom url say abc.com/page/?name=test&address=test. once copy-pasted in browser gets auto redirected to abc.com homepage. How to stop this auto-redirection in chrome or any browser?
You need to create an .htaccess file and use mod_rewrite to remove the file extension and create pretty URLs.
Something like the following to give you an idea, but you'll need to tailor the rules to suit your app's particular needs. Just Google "mod_rewrite" or "htaccess pretty" and you'll find tons of articles.
Options +FollowSymLinks
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* /index.php [L,QSA]

specific redirect required using mod_rewrite

I have to do a very specific url redirect using mod_rewrite within an .htaccess. Below is a url which has to map to the url below it:
m.example.com/123456/123456-product-name/
This needs to map to the following:
m.example.com/product-name/123456
I'm still getting to grips with regex and url rewrites and I've spent a good couple of hours trying to get this right. Can anybody help!?
Thanks in advance
You can do that. in root .htaccess:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^\d+/(\d+)-([^/]+) /$2/$1 [R=301,L]
Or just [L] (and not [R=301,L]), if you do that without redirection.

Joomla URL rewrite for component without a redirect

I have a Joomla component “com_foo” which works fine when called via...
http://my.joomla.site/index.php?option=com_foo&id=1234
... but I need the URL be in the following format:
http://my.joomla.site/getfoo/1234
The closest I can get is with the following .htaccess rewrite which causes a redirect:
RewriteEngine On
# My Rule
RewriteRule ^getfoo/(.*)$ /index.php?option=com_foo&id=$1 [R=301,L]
# Joomla rules
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteCond %{REQUEST_URI} !^/index\.php
RewriteCond %{REQUEST_URI} /component/|(/[^.]*|\.(php|html?|feed|pdf|vcf|raw))$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php [L]
I can’t seem to figure out how to make the same thing work “internally” w/o the redirect. So in my example above what happens is:
[1] GET http://my.joomla.site/getfoo/1234 REDIRECTS TO
[2] GET http://my.joomla.site/index.php?option=com_foo&id=1234 RENDERS PAGE
What I need is
[1] GET http://my.joomla.site/getfoo/1234 RENDERS PAGE
Almost everything I try results in a 500 error. I feel like it's got to be something simple but it's got me beat at the moment.
Although you could probably get this working using the .htaccess file, by seperating the SEF logic from the component code you open yourself up to a management nightmare down the road. Joomla does however, provide a means to manage SEF url parsing and generation with your custom components. By placing the file router.php in the root of your component's folder, Joomla will automatically use this file to both generate and parse SEF urls. Although the link below does provide detailed information on how to implement, I've found reverse engineering core components router.php file helped me tremendously.
http://docs.joomla.org/Supporting_SEF_URLs_in_your_component
Try:
RewriteEngine On
RewriteCond %{THE_REQUEST} ^(GET|POST)\ /index\.php\?option=com_foo&id=(.*)\ HTTP
RewriteRule ^ /getfoo/%2\? [R=302,L]
RewriteRule ^getfoo/(.*)$ /index.php?option=com_foo&id=$1 [L]
The above will:
Redirect http://my.joomla.site/index.php?option=com_foo&id=1234 to http://my.joomla.site/getfoo/1234 and show the information from the http://my.joomla.site/index.php?option=com_foo&id=1234 page
When viewing
http://my.joomla.site/getfoo/1234 will display the information from the http://my.joomla.site/index.php?option=com_foo&id=1234 page
Change the R=302 to R=301 when you are sure the redirect works.

Permalinks with mod_rewrite on custom site build

I am trying to get a pretty permalink system set up on a site I've just built and nothing is having any effect. I either get a 500 error or the permalinks don't work and/or it breaks all the links to CSS and images while still giving a 404 if you try to use the permalink structure.
Here is my .htaccess code:
RewriteEngine On
RewriteRule ^([^/]*)/([^/]*)/([^/]*)$ /main/index.php?act=$1&sub=$2&id=$3 [L]
I want it to convert links like index.php?act=admin&sub=edit-product&id=5 to /admin/edit-product/5 whilst including any in between. For example, if a URL has an ID call but not a sub call it will still work.
Any help would be greatly appreciated.
UPDATE:
I have added RewriteBase / and it has now doing something, but the permalinks are not working properly.
Is there a way I can find out where the rewritten URL is actually pointing?
It seems that full URLs that use all the rewrite rules work, but not the URLs that only need some of them.
I use this .htaccess code for rewrite in my projects:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php [NC,L]
This code pass exists request (for static files and etc) as is, but all other redirect to index.php. Request uri available int PHP as
$_REQUEST['REQUEST_URI']
In PHP we (or our framework) can parse it as we will.

Resources