How to make URL short in php? - php

In my index.php I have a list of properties. When I click on it, it directs me to property_detail.php but with extra details on the URL making it very long and untidy. How to make it short?

Set rewrite URL rule in .htaccess in the root folder.
RewriteEngine On
RewriteCond $1 !^(index\.php|assets|images|js|css|upload|favicon.png)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]

Related

how to change url rewrite by .htacces with .php extention

my website is,
http://localhost/mywebsite/page.php?id=123
using .htaccess i changes my url like this
http://localhost/mywebsite/newpostof2016
but i want like this final url
localhost/mywebsite/newpostof2016.php
current using .htacces code is
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([a-zA-Z0-9]+)$ page.php?id=$1
does this work? It should add .php to all files even if they arent php files
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.+)$ $1.php [L]
N.B. there are many reasons as to why you wouldn't want this behavior. I can't think of a case where this would benefit UX
This rule should do it.
RewriteEngine On
RewriteRule ^mywebsite/newpostof2016\.php$ /mywebsite/page.php?id=123 [L]

HTAccess Rewrite Issues

I am writing a website which is dynamically populated through an Oracle database.
I have completed the desktop site and am now required to create a mobile site. Due to how different the sites are planned to look, I have opted to create 2 different "template" like websites for the mobile and desktop sites.
However, for my desktop site, everything is built off the index.php file in order to allow it to be completely dynamic. Pages are therefore look like www.domain.com/index.php/page in the url.
For the desktop site, this works. I am using a generic index.php removal rewrite rule in order to then make the url www.domain.com/page however still display the same page as the previous URL.
My issue, is that now I have a www.domain.com/mobile/index.php. Which has been created and for the most part has been working, however when trying to add addition dynamic pages to the mobile site. www.domain.com/mobile/index.php/about for example just redirects to www.domain.com/mobile/ and it doesn't even include the about part of the URL.
After much debugging, I have discovered it is definitely the .htaccess that is causing the issue.
If you have any insight into my issue, please help me out.
Thanks in advance
EDIT 1
Rewrite Rules are as follows
# Removes index.php from ExpressionEngine URLs
RewriteCond %{THE_REQUEST} ^GET.*index\.php [NC]
RewriteCond %{REQUEST_URI} !/system/.* [NC]
RewriteRule (.*?)index\.php/*(.*) /$1$2 [R=301,NE,L]
# Directs all EE web requests through the site index file
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L]
You can use this code in your /mobile/.htaccess file:
DirectoryIndex index.php
RewriteEngine On
RewriteBase /mobile/
RewriteCond %{THE_REQUEST} /index\.php [NC]
RewriteRule ^index\.php(?:/(.*))?$ $1 [L,R=302,NC,NE]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ index.php/$1 [L]
This will override all the rules present in parent .htaccess for /mobile/ URI path.
Simplified version to make it work in root .htaccess:
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteRule ^(mobile)/(.*)$ $1/index.php/$2 [L,NC]
# Directs all EE web requests through the site index file
RewriteRule ^(.*)$ /index.php/$1 [L]
Based on the debugging you mentioned, there is a rule in your .htaccess which is rewriting www.domain.com/mobile/index.php/about to www.domain.com/mobile/. So, if you find which rule this is, you can add one above it that will catch requested URLs for your mobile pages and then not allow the problematic following rule to run. Something like this:
RewriteRule ^mobile/index.php/([a-zA-Z0-9-_]+)$ ^mobile/index.php/$1 [L,QSA]
The L ensures that if the user's request matches this rule, no further rules (including the one causing the issue) will be executed.
Thank you for the answers you've both given, however neither of them worked, I've now solved the issue, it was to do with the final RewriteRule at the end
# Directs all EE web requests through the site index file
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L]
I needed to change it to
# Directs all EE web requests through the site index file
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !/mobile/.* [NC]
RewriteRule ^(.*)$ /index.php/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} /mobile/.* [NC]
RewriteRule ^(.*)$ mobile/index.php/$1 [L]
So that it would work with both mobile and desktop sites.

Rewriting URL in CodeIgniter

I want to rewrite URL using .htaccess in codeigniter but it's not working. Can you please figure it out. Want to change URL from:
www.site.com/mauritius_holiday_rentals/apartment/anyname
to
www.site.com/mauritius_holiday_rentals/anyname
My current .htaccess file contains:
RewriteEngine on
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L,QSA]
RewriteRule ^mauritius_holiday_rentals/([A-Za-z0-9-]+)/?$ mauritius_holiday_rentals/apartment/$1 [L,QSA]
First four line is for removing index.php from URL which is working fine.
If routes file is set to access new url and you want to set redirection for old URLs then Use following code in .htaccess. otherwise let me know in detail what you want to do.
RewriteEngine on
RewriteBase /
RewriteRule /mauritius_holiday_rentals/apartment/$1 /mauritius_holiday_rentals/$1 [R=301,L]
Routes.php config file code
$route['mauritius_holiday_rentals/(:any)']="mauritius_holiday_rentals/apartment/$1";
Let me know if any problem.
RewriteEngine On
RewriteBase /yourProjectName/
RewriteCond $1 !^(images|stylesheets|javascript)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /yourProjectName/index.php?/$1 [L]
Try to use codeigniter routing system
https://www.codeigniter.com/user_guide/general/routing.html

How to use mod_rewrite to redirect requests of username but rest to a frontend index?

I am trying to redirect requests of username to a script but the all rest to a specific index called frontend.php using a .htaccess and a mod_rewrite.
Unfortunately, this failed and I keep struggling:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ frontend.php [QSA,L]
RewriteRule ^/user/([^/]+)$ profile/profile.php?u=$1 [QSA,L]
Thanks for your help!
You can use:
RewriteEngine On
RewriteRule ^user/([^/]+)$ profile/profile.php?u=$1 [QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ frontend.php [L]
.htaccess is per directory directive and Apache strips the current directory path (thus leading slash) from RewriteRule URI pattern.
Have you tried to swap the last 2 lines against each other?

.htaccess Save not existing url

Is there a way to save the not existing url?
Lets say i go to:
www.domain.com/channel/abcd1234/
The folder abcd1234 doesn't exitst so you will be redirected to:
www.domain.com/channel/error/
Is there a way to save the first url? and getting it on the error page in a php variable?
<ifmodule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) http://domain/channel/error/ [R=301,L]
</ifmodule>
Instead of your rewrite rule you should use:
ErrorDocument 404 /channel/error/
Original 404 URL will be available to you as $_SERVER['REQUEST_URI']
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^([^/]+)$ $1.php [L]
replace by it.i wish it help you

Categories