Given the PHP URL course.php?id=10 I want it to redirect to course.html and I want to use the id=10 on this page. How can I do this?
You can't cleanly rewrite the url and not include the variable somewhere. You'll need to do something like so:
RewriteRule ^([0-9]+)/([^.]+)\.html $2.php?id=$1 [L]
Which will work for http://example.com/10/course.html.
Create a .htaccess file and add the following:
RewriteEngine On
RewriteRule ^([^.]+)\.html$ $1.php [L]
Upload this to the root of your files (most of the time it's /var/www/ or /home/user/public_html/.
Related
I want to redirect every .htaccess call to a particular file.
I want to redirect all Get/Put/Post/Delete calls to a folder to a specific index.php file.
I have following directory structure:
\var\www\html\app
inside app i have a file index.php
Now i want any calls like
GET http(s)://myWeb.com/app
POST http(s)://myWeb.com/app
GET http(s)://myWeb.com/app/helloworld
to redirect to
\var\www\html\app\index.php
Here is my failed attempt:
RewriteEngine On
RewriteBase /app/
RewriteRule ^ app/index.php [QSA,L]
#RewriteRule ^ index.php [QSA,L] //Tried this one as well
This .htaccess is inside
/var/www/html
I also placed a similar htaccess inside app folder but it doesnt works too.
Should i use 2 htaccess files ? If one what should be the format for it ?
Also, is there a tool to check htaccess regex ? something like www.regexr.com
Did you try this?
RewriteRule . app/index.php [R,L]
I think QSL is needed only if you want to pass the URL queries
I have a single point of entry on my website
mysite.com/admin/index.php?View=List&Model=User
All access to the DB/application is through this index.php file.
I would like to clean my URL from what is shown above to.
mysite.com/admin/List/User
The idea is to remove the key 'Model and 'View'.
I have found this link that looks very similar.
PHP/Apache: Rewrite rules with .htaccess
in htaccess file insert this code :
RewriteEngine On
RewriteRule ^admin/([^/]*)/([^/]*)$ /admin/index.php?View=$1&Model=$2 [L]
The rewritten URL:
http://mysite.com/admin/List/User
this site very useful for generate rewrite URL
http://www.generateit.net/mod-rewrite/
Create a file named .htaccess in your web root if you haven't already got one with the following:
# Turn the rewrite engine on (skip this if already done)
RewriteEngine On
# Redirect admin/List/User/ to admin/index.php?View=List&Model=User
RewriteRule ^admin/([^/.]+)/([^/.]+)/?$ admin/index.php?View=$1&Model=$2 [L]
This might work:
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^admin/([^/]+)/([^/]+) /index.php?View=$1&Model=$2 [NC]
Add to .htaccess in the root folder of your website.
I've managed to remove the need for typing index.php in my urls, but it is still possible to do so. I don't want that and want to prevent users to be able to access my application via urls like /index.php/home or /index.php/contact or even /index.php.
Does anyone know how to accomplish this?
In you .htaccess write in the top
DirectoryIndex my_new_index.php
RewriteEngine on
RewriteCond $1 !^(my_new_index\.php|images|robots\.txt)
RewriteRule ^(.*)$ /my_new_index.php/$1 [L]
Rename your index.php file to my_new_index.php
Exactly. Here's an example:
RewriteRule ^foo\.html$ bar.html
Or you can use variables like so:
^hotsheet/(.*)$ http://www.tstimpreso.com/hotsheet/$1
If you're using Apache, then just setup a rewrite rule to redirect users from index.php to /
RewriteRule ^/index\.php$ /
RewriteRule ^/index\.php/(.*) /$1
I have added a .htaccess file to my root folder, and i wanted everything written after the / to be sent to the index.php file as get data.
My root path looks like this http://www.site.com/folder/ and my .htaccess is located in the folder directory together with index.php
This is my .htaccess file:
Options +FollowSymLinks
RewriteEngine on
RewriteRule (.*) index.php?args=$1
Now, what ever i write behind folder/ in my url, args is "index.php". So when i visit www.site.com/folder/lots/of/bogey the args variable is "index.php"
My goal is obviously to have the args variable be "lots/of/bogey". Any ideas what I'm doing wrong?
You don't need a RewriteCond. The following will work:
RewriteRule ^(.*)$ index.php?args=$1 [L,QSA]
The L makes it stop matching rewrite rules, and QSA is for appending to query string in a rewrite rule. Refer to mod_rewrite
I think that's because after executing the RewriteRule and getting index.php?args=... the RewriteRule gets called again. Now index.php is your filename, so it get's passed as args. After this mod_rewrite aborts due to recursion. To fix this, add a RewriteCond which enures the file isn't index.php.
You'll have at least to exclude index.php from the redirect:
RewriteCond $0 !^index\.php$
RewriteRule .* index.php?args=$0 [QSA,B]
I would like to take requests for /somefolder/style.css and handle them with /somefolder/program.php
So, I put the following in my .htaccess file:
rewriteengine on
rewriterule ^style.css$ program.php?css=1 [R=302,L]
The result is that instead of redirecting to /somefolder/program.php, the server tries to redirect to:
/var/www/html/somefolder/program.php?css=1
How can I get rid of the /var/www/html/ in the redirect? I thought that since I just entered program.php in the .htaccess that it would default to the same folder.
Since this is a generic script that I will use in many places, I would like to avoid using rewritebase to specify the folder I'm in -- the .htaccess has to work in any folder without being modified.
Leave the R flag away and you will get an internal redirect:
RewriteRule ^style\.css$ program.php?css=1 [L]
Otherwise specify the full URL path you want to redirect to externally:
RewriteRule ^style\.css$ /program.php?css=1 [R=302,L]
Or for any arbitrary folder:
RewriteCond %{REQUEST_URI} (.*)/style\.css$
RewriteRule ^style\.css$ %1/program.php?css=1 [R=302,L]
I think the problem is that you are missing a ReWrite base statement.
Also the I would put the .htaccess file in the root directory of your site. That way you don't have to copy an .htacess file into every new directory you create. What if you want to change the name of your php file? You'd have to change every .htaccess file.
This is how I would redirect www.mydomain.com/orange/style.css to www.mydomain.com/orange/program.php?css=1 using generic rules:
RewriteEngine On
RewriteBase /
RewriteRule ^(.*)/style\.css$ $1/program.php?css=1 [L]