How can I change a url of the following format
example.com/page/1
To
example.com/index.php?page=1
?
When I enter
example.com/page/1
it should redirect to
example.com/index.php?page=1
What changes do I need to do in my .htaccess file?
folder structure is as follows
-Public_html
.htaccess
index.php
Thanks.
Use this in your public_html/.htaccess file
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^page/([0-9])/?$ /index.php?page=$1 [QSA,NC,L]
RewriteCond checks if the requested filename or directory already exist, RewriteRule will be skipped.
You can put this code in your htaccess
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^page/([0-9]+)$ index.php?page=$1 [NC,L]
With only this code, you can now access http://example.com/page/55 and see the content of /index.php?page=55 for instance.
But... the thing is, you're still able to access http://example.com/index.php?page=55 and it creates a duplicate content: very bad for referencing (Google, and others).
More info about duplicate content: here.
Solution: you can add another rule to redirect http://example.com/index.php?page=55 to http://example.com/page/55 (without any infinite loop)
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} \s/index\.php\?page=([0-9]+)\s [NC]
RewriteRule ^ page/%1? [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^page/([0-9]+)$ index.php?page=$1 [NC,L]
Note 1: make sure (in Apache configuration file) that you've enabled mod_rewrite and allowed htaccess files.
Note 2: since your rule creates a virtual directory (/page/) you'll have some problem if you're using relative paths for your html resources. Make sure all your links (js, css, images, href, etc) begins with a leading slash (/) or instead, add a base right after <head> html tag: <base href="/">
in my answer I assume you are using linux,
I also assume you will have more complicated cases such as more than
one parameters you will want to catch
example.com/page/1/3
in this case I think you will have to use parsing the url in your index.php
first you will have to setup the .htaccess file in your site root, also you will have to make sure mod_rewrite is enabled in your apache configuration
in case you are running debian you can run this command in terminal
to make sure this mod is enabled:
sudo a2enmod rewrite
add htaccess file to the root of where your index php file is located example:
/var/www/html/.htaccess
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^.*$ ./index.php
</IfModule>
according to my solution in your index.php you must have function that can parse the url request
/*
$base path is used only if you running your site under folder
example.com/mysitefolde/index.php
*/
function getUrlParams($basePath = ''){
$request = str_replace($basePath, "", $_SERVER['REQUEST_URI']);
return explode('/', $request);
}
index.php request to example.com/page/1/2
$request = getUrlParams($rootPath);
$module = $request[0]; // page
$moduleValue = $request[1]; // 1
$moduleValue2 = $request[2]; // 2
Related
I have index.php that reads full path by $_SERVER[‘REQUEST_URI’] variable. My task is when user enter: www.domain/resource/777 redirect to index.php with path /resource/777 and parse $_SERVER[‘REQUEST_URI’] to do some logic. But I have also real files and folders like:
css/theme.css
assets/assets.js
resource/locale.js
When I try this config:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /index.php [L]
Client could not get all css and etc files. How to write correct rule in Apache to achive this?
If you are using Apache 2.2.16 or later, you can replace rewrite rules entirely with one single directive:
FallbackResource /index.php
See https://httpd.apache.org/docs/2.2/mod/mod_dir.html#fallbackresource
Basically, if a request were to cause an error 404, the fallback resource uri will be used to handle the request instead, correctly populating the $_SERVER[‘REQUEST_URI’] variable.
Your rule is fine. Issue with css/js/image is that you're using relative path to include them.
You can add this just below <head> section of your page's HTML:
<base href="/" />
so that every relative URL is resolved from that base URL and not from the current page's URL.
Also keep just this rule in your .htaccess:
DirectoryIndex index.php
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^[^.]+$ index.php [L]
According to the documentation REQUEST_FILENAME only evaluates to the full local filesystem path to the file if the path has already been determined by the server.
In a nutshell this means the condition will work if it is inside an .htaccess file or within a <Directory> section in your httpd.conf but not if it's just inside a <VirtualHost>. In this latter case, you can simply wrap it in a <Directory> tag to get it working.
<VirtualHost *:80>
# ... some other config ...
<Directory /path/to/your/site>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /index.php [L]
</Directory>
</VirtualHost>
You can set the error document in your htaccess file so that index.php will be used then a non existing file is requested:
ErrorDocument 404 /index.php
For now only this helped to me:
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/src/
RewriteCond %{REQUEST_URI} !^/assets/
RewriteCond %{REQUEST_URI} !^/resource/
RewriteCond %{REQUEST_URI} !^/data.json
RewriteRule ^(.*)$ /index.php [L]
I have learned and make website using CodeIgniter. I want to create this system module base so I have created multiple folders and controller,views and model for each module and it's working fine. I want to remove index.php and hide query string like pass user id in URL. I have tried htaccess code which given user guide but it did not work.
Current URL :
mysite/sitename/index.php/departments/departments/delete_department/14
Require :
mysite/sitename/departments/departments
I have tried below code in htaccess but it does not work as expected, it displays page not found error.
RewriteEngine on
RewriteCond $1 !^(index\.php|images|fonts|js|css|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]
My directory structure is something like that
My site
|_application
| |_ Controller
| |_Login (folder) // module folder
|_Models
| |_pages
|
|_views
|_login (folder) // module folder
1) you should have Rewrite mod on apache
2) You should change application/config/config.php
$config['index_page'] = 'index.php'; to: $config['index_page'] = '';
3) after that I see that you are having a subdirectory of sitename so you should add that on your .htaccess as your RewriteBase which will result in an .htaccess like this:
RewriteEngine On
RewriteBase /sitename/
RewriteCond $1 !^(index\.php|resources|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
If i understand your folder structure right, then your root folder is mysite and your CI application is located at mysite/sitename . if this is the case try checking out the last part of the rewrite
RewriteRule ^(.*)$ /index.php/$1 [L]
As it currently tries to load an index file that is located on your root folder (mysite) and under my understanding your index file is actually located on mysite/sitename, so you can try this:
RewriteRule ^(.*)$ /mysite/index.php/$1 [L]
Using your FTP client, create a new file named .htaccess (including the leading dot) in the same folder as your site’s main index.php file.
Then add the following code to this newly created .htaccess file:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
# 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]
</IfModule>
If your site’s system directory (/system/) has been renamed and is still accessible by URL, modify the RewriteCond line above:
RewriteCond %{REQUEST_URI} !/newdirectoryname/.* [NC]
If you are running EE from a sub-directory rather from the root of your domain (e.g. http://example.com/myeesite/ instead of http://example.com/), just remove the slash preceding index.php in the RewriteRule line above, like so:
RewriteRule ^(.*)$ index.php/$1 [L]
If you are running EE from a sub-directory and it still doesn’t work after removing the slash, you may need to specify the sub-directory in your rewrite rule. For example, if your sub-folder is named testing, change:
RewriteRule (.*?)index\.php/*(.*) /$1$2 [R=301,NE,L]
To:
RewriteRule (.*?)index\.php/*(.*) testing/$1$2 [R=301,NE,L]
And change:
RewriteRule ^(.*)$ /index.php/$1 [L]
To:
RewriteRule ^(.*)$ testing/index.php/$1 [L]
If your host requires forcing query strings, try adding a question mark following index.php in the RewriteRule line above, like so:
RewriteRule ^(.*)$ /index.php?/$1 [L]
do you have apache rewrite-mod enable ?
you must have apache rewrite-mod enabled to do that ..
Check your config at apache configuration path ..
Try this
RewriteEngine on
RewriteCond $1 !^(index\.php|resources|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]
change $config['index_page'] = 'index.php'
to
$config['index_page'] = ''
from
application\config\config.php
Also check your apache config for mod rewrite is enabled or not.
Checking URl Rewrite is Working or Not
MOD Rewrite
$isTrue = in_array('mod_rewrite', apache_get_modules());
Unfortunately, you're most likely trying to do this with CGI, which makes it a little bit more difficult.
You can test it using the following, though
$isTrue = strpos(shell_exec('/usr/local/apache/bin/apachectl -l'), 'mod_rewrite') !== false
If the above evaulates to true, then mod_write is enabled.
You are using the rewrite engine of apache. Make sure mod_rewrite is enabled and make sure your regular expression fulfills your needs.
Read about mod_rewrite here.
The htaccess rules look OK. Because you havent shown your code its hard to say but its either one of the following:
Your having an issue with your controller and method both being called 'departments'. If this is true either try renaming the method to something other than 'departments' or getting rid of it all together.
You dont have a controller called 'departments' (it doesnt show in your directory structure) or a route to handle the URI 'departments'. Either create the controller or add a route to direct the URI 'departments' to the controller you want to use. ie:
$route['departments/departments']='an_existing_controller/an_existing_method';
You dont have a method called 'departments'. If this is true get rid of the additional URI component called 'departments' or create a new method (as above dont call your method the same thing as your controller).
In config.php (which is mostly loated in application/config/config.php
make index page value to empty string, like this,
$config['index_page'] = '';
In root folder of your application make a .htaccess file with following code,
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]
</IfModule>
So to begin with I have a custom url rewrite that sends a request variable to a php script
Rewrite rule is below:
RewriteRule ^([\w\/-]+)(\?.*)?$ test/index.php?slug=$1 [L,T=application/x-httpd-php]
So if you access something like domain.com/slug-text it sends slug-text to index.php located in folder named test.
What I want is all my urls to look like domain.com/slug-text.html, but slug-test variable should still be sent to index.php file.
And
What I can't figure out is the redirect. I want all the old urls to be redirected from domain.com/slug-text or domain.com/slug-text/ to domain.com/slug-text.html and slug-text sent to index.php file located in test folder.
Searched a lot but could not find the answer for this question anywhere on the Internet.
Thank you all for the help.
UPDATE:
my new code is:
RewriteEngine On
Options +FollowSymlinks
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-l
RewriteRule ^(([\w/\-]+)?[\w-])(?!:\.html)$ http://domain.com/$1\.html [L,R=301]
RewriteRule ^(([\w/\-]+)?[\w-])(/|\.html)?$ test/index.php?slug=$1 [L]
domain.com/slug-text/ does not get redirected to domain.com/slug-text.html
domain.com/slug-text works as intended redirecting to domain.com/slug-text.html
What do i need to change?
This rule:
RewriteRule ^(([\w/\-]+)?[\w-])(/|\.html)?$ test/index.php?slug=$1 [L]
Will trap domain.com/slug-text, domain.com/slug-text/ and domain.com/slug-text.html and send slug-text to /test/index.php inside slug param.
If you really want to redirect using [R=301] from old urls to new then use this:
RewriteRule ^(([\w/-]+)?[\w-])/?(?!:\.html)$ http://domain.com/$1.html [L,R=301]
RewriteRule ^(([\w/-]+)?[\w-])\.html$ test/index.php?slug=$1 [L]
Also note that as using explicit redirect bottom rule is modified to trap url's ending with .html
It is also advisable (if your .htaccess does not already contain this) to filter conditions for existing files and folders not to be trapped by your redirect rules. Simply add these lines before RewriteRule lines:
# existing file
RewriteCond %{SCRIPT_FILENAME} !-f
# existing folder
RewriteCond %{SCRIPT_FILENAME} !-d
And if using symlinks:
# enable symlinks
Options +FollowSymLinks
# existing symlink
RewriteCond %{SCRIPT_FILENAME} !-l
// addition
Your .htaccess file should look like this:
RewriteEngine on
Options +FollowSymLinks
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-l
RewriteRule ^(([\w/-]+)?[\w-])/?(?!:\.html)$ http://domain.com/$1.html [L,R=301]
RewriteRule ^(([\w/-]+)?[\w-])\.html$ test/index.php?slug=$1 [L]
This is supposed to redirect /slug-text to /slug-text.html
RedirectMatch ^/([\w-]+)/?$ http://domein.com/$1.html
This is in the case when slug-text is only letters, digits, – and _.
Rewrite slug-text.html to a php file and pass the slug as a param:
RewriteRule ^([\w-]+)\.html$ test/index.php?slug=$1 [R,L]
If you have both line in your .htaccess the first one will do the redirects from the legacy URLs to the new ones and the second one will process the request.
I am currently making a website (http://tannernelson.me/ehs)
It's hosted on Godaddy, and I'm using wordpress as a CMS.
I want to be able to make:
http://tannernelson.me/ehs/school/academics/teachers/jsmith
turn into
http://tannernelson.me/ehs/index.php?pagename=teachers&detail=jsmith
So, basically, if there are 4 segments to the url (school/academics/teachers/jsmith) I want the last one to be a variable. So the fourth segment of any url will be the variable "detail"
My current URL rewrite is currently
# Mod Rewrite
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /ehs/index.php [L]
Options -Multiviews
It won't work any other way, even with the default WordPress .htaccess file. And I have no idea what that means, or what kind of request URI is made out of it. It's really confusing.
Any ideas?
The code you have right now means:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
If the requested filename is not (!) a regular file -f and if the requested filename is not (!) a directory -d then:
RewriteRule . /ehs/index.php [L]
Match any single character (.) and if a match is found rewrite the URL to /ehs/index.php and then make this the last rule ([L]) so don't process any further rules.
This doesn't look like what you want, but seems to be working. http://tannernelson.me/ehs/school/academics/teachers/jsmith serves up (I think) http://tannernelson.me/ehs/index.php because I get a custom 404 not found page.
Try the following .htaccess code:
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
# Redirect the ehs/school/academics/$1/$2 URIs to /ehs/index.php?pagename=$1&detail=$2
RewriteRule ^ehs/school/academics/([^/]+)/([^/]+)$ /ehs/index.php?pagename=$1&detail=$2 [L]
# Otherwise if the requested URI is not found (not a file nor a directory)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
#Redirect everything else to index.php
RewriteRule .* /ehs/index.php [L]
Options -Multiviews
I just tested this on my Apache server and it works.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]
</IfModule>
can any body explain me how above htaccess rule works
e.g. if I have URL http://mydomain/edituser
so what php script will match with given URL
earlier I write different rules for each given URL
but in above case how I know that witch php script get run
please help me
That rewrite rule matches any request that does not match an existing file, and routes the request to to index.php using PATH_INFO
Translation of the above code is like that:
RewriteEngine On: Activate the RewriteEngine if not already activated
RewriteCond %{REQUEST_FILENAME} !-f: If the requested file name is not a regular file
RewriteCond %{REQUEST_FILENAME} !-d: If the requested file name is not a regular directory
RewriteCond $1 !^(index\.php|images|robots\.txt): If the request is not the index.php, the images or robots.txt file
RewriteRule ^(.*)$ /index.php/$1 [L]: Send the request to index.php and stop ([L])
This looks as a part from WordPress. If the file doesn't exists (-f) and a directory also not exists (-d) and the request is not for index.php or images or robots.txt when call index.php with the path as a parameter.