I have created a real estate website and on it I want to add some rewrite rule to make urls seo friendly. Mainly I want to do this on property details page. Right now the property details page URL is like this
http://www.my-website.com/property_details.php?id=11&slug=westbourne-grove-serviced-offices
So I am passing the property ID and a Slug text of the property name. This slug is not being used anywhere at the moment, so the property details are going to be fetched only by using the ID.
Now I want to change the URLs to
http://www.my-website.com/property/westbourne-grove-serviced-offices-11
So the slug text comes first and the property id is merged in the end using dash "-". I have no knowledge on creating rewrite rules. Any help will be appreciated.
Thank You
You can use this code in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
RewriteBase /
RewriteRule ^property/(.+?)-([0-9]+)/?$ property_details.php?id=$2&slug=$1 [NC,L,QSA]
Related
I have a few urls which contain book-category in the url. For example, https://www.example.com/abc/book-category/book-1/
I want to load the page in the browser without book-category in the url but still want to load the page content, e.g. if I go to https://www.example.com/abc/book-1/ I want to see the content of https://www.example.com/abc/book-category/book-1/ but still keeping https://www.example.com/abc/book-1/ in the url without redirecting. Is this something I can achieve with some htaccess rules
I understand that "book-category" is a literal, static string ...
That means this rule should work for you:
RewriteEnginge on
RewriteRule ^/?abc/book-1/?$ /abc/book-category/book-1/ [L]
Often one wants something more generic, though, assuming that "abc" is something fixed you can anchor your rule on:
RewriteEnginge on
RewriteRule ^/?abc/([^/]+)/?$ /abc/book-category/$1/ [L]
Or maybe both parts of the URL have dynamic character?
RewriteEnginge on
RewriteRule ^/?([^/]+)/([^/]+)/?$ /$1/book-category/$2/ [L]
In the end on what exactly you want to achieve. Your question is vague in that, so I can only make suggestions. Sorry for that.
HellŠ¾, I have a user's profile page, which can be accessed by
root/profile.php?user=batman
I want to make a cleaner link, like
root/profile/batman
I know that it is possible to do with mode rewrite in htaccess, which would read root/profile/batman as root/profile.php?user=batman. The problem is whatever I tried doesn't work. Can someone show me the correct rewrite statement.
Thank you!
Assuming user name will include only alphabets. This rule will work
RewriteRule ^root/profile/([a-z]+) root/profile.php?user=$1 [L]
Is there a way to deny access to a file using one URL, but allow access using another?
Example:
The actual URL is www.domain.com/index.php but I want to refuse access using that URL, but I still want to be able to display the file using www.domain.com/index.php/somestring
Is this possble?
Instead of generating the .htaccess with the strings stored in the DB, you would be better off writing the .htaccess file so that it treats "somestring" as a GET variable. You would then check if "somestring" is in your DB and if it is, you show the page. Otherwise, you do a php header redirect.
.htaccess
RewriteRule ^index.php/([^/]*)$ /index.php?id=$1 [L]
In your index.php first, grab the GET variable, check for it's presence in the DB, and if it is present, then show the page. Otherwise, you can use a header redirect to send the user to a different page.
$id = $_GET['id']; // You will probably want to escape this
Then, wrap the entire page in an if statement and only display the page if the id exists in the database.
I am very newbie to this. I have a form that has 4 fields. Field1 - 4 method GET. so when i submit the form The url becomes
mydomain.com/page.php?field1=f1&field2=f2&field3=F3&field4=f4.
I want to change this to
mydomain.com/newPath/f1/f2/F3/f4/
You actually want a redirect then. You would rewrite
domain.com/path/a/b/c/d
to
domain.com/path/?w=a&x=b&y=c&z=d
But if you want the server to direct a request like this
domain.com/path/?w=a&x=b&y=c&z=d
to one like this, you are actually redirecting users.
domain.com/path/a/b/c/d
You want a 301 redirect, because then it will hide the new URL with the ampersands:
This is called clean URLs and you can achieve it with regex that accomodate your need in the htaccess file:
Clean URL htaccess
So I have a problem in which I'm attempting to redirect a URL based on whether or not it begins with a certain numbers. I'm needing to redirect a site visitor if the first character of the trailing URL begins with the number "1", then I need to remove the number 1 of the URL and then replace the 1 with a specific directory path.
So here's a specific example to clarify. If the user clicks on (or types in their browser's address bar)
http://www.example.com/1d39g
they would automatically be redirected to
http://www.example.com/product/d39g
Also worth nothing, they individual typing in this URL will be reading it off a label in the mail, they won't always be clicking on a link. I originally had thought of doing this through jQuery, but I thought .htaccess would be the best way to approach this.
Activate the mod alias, then add this into your .htaccess
RedirectMatch /1(.*)$ /product/$1