I would like to make a rewrite rule for my website but I cannot seem to get the proper code in order to make this work.
The current URL of my website is looking like http://www.mohanadarafe.io/JSON/json.html
I want it to be: http://www.mohanadarafe.io/json
I have tried the following code but it does not seem to work:
RewriteEngine On
RewriteRule ^\.html$ /json [L]
Any idea how to fix this?
You have it reversed. Have it like this:
RewriteEngine On
# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} /JSON/json\.html [NC]
RewriteRule ^ /json [R=301,L]
# internal forward from pretty URL to actual one
RewriteRule ^json/?$ JSON/json.html [L,NC]
Then you can have your URL as: http://www.mohanadarafe.io/json
Related
I am trying to make a link that looks like https://www.exapmle.com/profile.php?u=8 to look like https://www.exapmle.com/profile/8
I have a tried variations of this in htaccess:
RewriteRule ^/profile/([0-9]+)/?$ profile.php?u=$1
RewriteRule ^/profile/([0-9]+)\.html /profile.php?u=$1
I don't know what i am doing wrong, the links don't change and I'm not getting any errors either
You may use this code in your site root .htaccess:
Options -MultiViews
RewriteEngine On
# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} /profile\.php\?id=([^\s&]+) [NC]
RewriteRule ^ /profile/%1? [R=301,L]
# internal forward from pretty URL to actual one
RewriteRule ^/?profile/(\d+)(?:\.html)?/?$ profile.php?u=$1 [L,QSA,NC]
I've never needed to use a .htaccess before and I'm fairly new to coding I'm trying to get localhost/index.php?id=123456789 to be passed as localhost/123456789/ but I just cant get the HTaccess right, i've tried everything I could find from prevoius posts here, haha!
My current HTACCESS looks like this, however it doesnt do what I want.
RewriteEngine On
RewriteRule ^id/(\d+)$ /index.php?id=$1 [NC,QSA,L]
You can do it with mod_rewrite in .htaccess however I'm not sure from your question which direction you want it to be passed to.
If you want people to type the php script in the URL bar you want:
RewriteEngine on
RewriteCond %{QUERY_STRING} id=([0-9]+)
RewriteRule ^index.php$ %1/
Or if you want people to enter the "pretty" version but for your server to load the PHP script you need:
RewriteEngine on
RewriteRule ^([0-9]+)/$ index.php?id=$1&%{QUERY_STRING}
For both ways and 301 redirect to pretty URL:
RewriteEngine on
RewriteRule ^([0-9]+)/$ index.php?id=$1&%{QUERY_STRING} [L]
RewriteCond %{QUERY_STRING} id=([0-9]+)
RewriteRule ^index.php$ %1/ [L,R=301]
How could I rewrite my URL using HTACCESS?
When a visitor visits index.php, I want the URL to be rewritten to something else, although the visitor will remain on the index.php page.
This is what I've tried (I did research this before asking but couldn't solve it myself):
RewriteEngine on
RewriteRule ^index.php$ testingit.php
Basically, I just wanted to change index.php to 'testingit.php', just to see if it would work.
You can use this code in your testwebsite/.htaccess file:
RewriteEngine On
RewriteBase /testwebsite/
# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} /index\.php[?\s] [NC]
RewriteRule ^ home [R=302,L,NE]
# internal forward from pretty URL to actual one
RewriteRule ^home/?$ index.php [L,NC]
So here's how you do it:
redirect *** /index.php http://www.yoursite.com/testingit.php
You need to replace *** with one of the following:
301-For a permanent redirect meaning browsers update bookmarks etc.
or
302-For a temporary redirect
Here's a link to a guide I found:
https://www.branded3.com/blog/htaccess-mod_rewrite-ultimate-guide/
Hope this helps :)
To make pretty URL's:
RewriteEngine On # Turn on the rewriting engine
RewriteRule ^home/?$ index.php [NC,L]
I'm having some trouble using the right code for my .htaccess file.
What I'm trying to accomplish is this:
We have a QR code generator which generates random url'Ss like this:
http://mydomain.com?APP-V2/7091c104-32a8-4680-9c07-a75c3ee61d7a/00
I need to redirect all these url's to the homepage, http://mydomain.com.
How to I write the wildcard in my htaccess file? Basically everything after mydomain.com?APP-V2/ should be redirected.
Any help much appreciated!
Basically everything after mydomain.com?APP-V2/ should be redirected.
If you want:
http://mydomain.com/?APP-V2/7091c104-32a8-4680-9c07-a75c3ee61d7a/00
to be redirected to:
http://mydomain.com/
Then you just get rid of the query string (e.g. ?APP-V2/7091c104-32a8-4680-9c07-a75c3ee61d7a/00):
RewriteEngine On
RewriteCond %{QUERY_STRING} ^APP-V2/
RewriteRule ^$ /? [L]
But if you want everything after the ?APP-V2/, you need this:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^APP-V2/(.*)$
RewriteRule ^$ /%1? [L]
I've wrote .htaccess file to convert urls to SEO friendly :
the original url is :
http://palestinianz.com/?page=person&p=Alex-Atalla
the content of .htaccess is :
RewriteEngine On
RewriteRule ^([^/]*)\.html$ /?page=person&p=$1 [L]
it should produce a link like this :
http://palestinianz.com/Alex-Atalla.html
But it makes no effect although I put the file in the root of my website !
where is the problem ?
If you want the URL to change in the address bar, you need to redirect the browser, then rewrite on the server. The first part of this answer explains the process that you want: https://stackoverflow.com/a/11711948/851273
So looking at the second list, the process is to redirect the browser if an ugly link is request to a nice looking link:
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /\?page=person&p=([^\ ]+)
RewriteRule ^$ /%1.html? [R=301,L]
Now, on the server end you need to internall rewrite it back to the ugly url, which is more or less what you already have:
RewriteRule ^([^/]*)\.html$ /?page=person&p=$1 [L]
You can try:
RewriteCond %{REQUEST_URI} (.+)\.html$
RewriteRule ^(.+)\.html$ ./index.php?page=person&p=$1 [L]
Only problem with this is any .html page will go through this. May want to change to the following:
# URL: domain.com/person/Alex-Atalla.html
RewriteCond %{REQUEST_URI} (.+)/(.+)\.html$
RewriteRule ^(.+)/(.+)\.html$ ./index.php?page=$1&p=$2 [L]
This will allow you to have more robustness in changing the the page variable as well