I have came accross a problem that every .htaccess query I've tried wasn't worked out ! I have URLs like this:
http://www.example.com/index.php?x=product
And I want to change it to a user friendly URL like this:
http://www.example.com/product/
Or it can be:
http://www.example.com/product.php
I've tried this code below:
RewriteEngine on
RewriteCond %{QUERY_STRING} ^x=product$
RewriteRule ^index.php.*$ http://www.example.com/product.php? [R=302,L]
Now, it is redirecting perfectly, but this is not the problem. I've used this for only SEO so I must include http://www.example.com/index.php?x=product in the product.php file. Any help can be precious, thanks...
You can use this code in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} /index\.php\?x=([^\s&]+) [NC]
RewriteRule ^ /%1/? [R=302,L,NE]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/.]+)/?$ index.php?x=$1 [L,QSA]
This will redirect /index.php?x=product to /product/ and will rewrite it internally to /index.php?x=product in 2nd rule.
You don't need to put anything in the product.php file. Make sure there is a .htaccess in the directory that has the files you want to make url/seo friendly.
To have it SEO friendly make sure its in this format
http://www.example.com/product/ not http://www.example.com/product.php
if you must have a file extension, have it in http://www.example.com/products.html (you want to tell the search engine the page isn't dynamic although it is to get better pagerank)
Insert this in your .htaccess
RewriteRule /(.*)/$ products.php?x=$1
the (.*) pulls the elements out and puts them in variables $1
Related
One pages of site have such adds: domain.com/posts/name.php
Other pages of site have such adds: domain.com/pages/name.php
I need to cut from these adds posts/ and pages/ only.
Firstly, I tried to use in .htaccess next rules:
RewriteRule ^([A-z0-9-]+)$ /posts/$1
RewriteRule ^([A-z0-9-]+)$ /pages/$1
Don't help.
Secondly, I tried to use in .htaccess such rules and conditions:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)$ posts/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)$ pages/$1 [L]
As result: image of mistake 404
Also I tried to use such method:
RewriteCond %{REQUEST_URI} ^([A-z0-9-]+)$/(posts|pages)/([A-z0-9-]+)/$1 [NC]
RewriteRule .* domain.com/%1%3
Don't help....
what i am understanding you need to remove the post and pages from the url following code will help you for that.
You would need the rewrite module of Apache: mod_rewrite.
Then do something like this:
RewriteEngine on
RewriteRule ^post/(.*)$ $1
RewriteRule ^pages/(.*)$ $1
Here is the official documentation of mod_rewrite: click
please test it onece I dint test the code but did same for replacing url and its work for me.
Thanks.
The solution of this problem such:
We enter all information about the page in the MYSQL database:
image of database
We generate a page from the database:
generation of page (php)
Set the generated page for Human-Friendly Url:
file .htaccess
So everything works, and POSTS there are in their folder in the database, and PAGES in their folder.
folder in database
All is clean. If you can do better somewhere, then correct me, please.
We have added a paging system inside our layout. When we go to /page/clan, the page about our clan gets displayed. (as its located in pages/clan.php).
To get /page, we used a htaccess script, which rewrites index.php?page=pagename into the /page/pagename I mentioned.
This is our current htaccess code for converting these urls:
RewriteEngine On
RewriteRule ^page/([^/]*)$ index.php?page=$1 [L]
However, We'd like to remove the /page part, so it's possible to just use /clan instead of /page/clan to open the clan page.
How can this be done and with what code?
Thanks!
Try :
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]*)/?$ index.php?page=$1 [L,NC]
Rewrite condions make sure you don't rewrite any existing files or directories on the server.
[NC] flag makes the pattern case insensitive, so in you case example.com/fOo would also work.
OK I'm new with the issue of URL rewriting and redirecting. I've search up and down on Google & Stackoverflow for an answer that would work and nothing seems to work!!
What i'm trying to accomplish is...
Take this link: mysite.com/research/index.php?quote=goog
Then convert it and redirect it to this: mysite.com/research/goog
Does it matter that the "goog" at the end of the URL string is grabbed from a form placed in the url? here is the code used to grab the "goog" <?php echo $_POST['quote'];?>
Below is the only snippet code that I have on my htaccess file and it won't work! Am I doing it wrong? Is there something missing from my code? I have my code place on the root directory (mysite.com) should i have it in the "research" folder? (mysite.com/research/)
RewriteEngine On
RewriteRule ^quote/([^/]*)$ /research/index.php?quote=$1 [L]
Is it possible my host / server doesn't accept .htaccess files? should I do it in a web.config file? If so how would I convert the above code to a working web.config file?
These are the rules you will need in your /research/.htaccess file:
RewriteEngine On
RewriteBase /research/
RewriteCond %{THE_REQUEST} /index\.php\?quote=([^\s&]+) [NC]
RewriteRule ^ %1? [R=301,L,NE]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/?$ index.php?quote=$1 [L,QSA,NC]
Access it in your index.php using:
$quote = $_GET['quote'];`
This should work for you:
RewriteEngine On
RewriteCond %{QUERY_STRING} "quote=([^&]+)"
RewriteRule ^/research/index.php /research/%1? [R,L]
The query string is a separate part of the request to the URL so you need to check that explicitly. By using the RewriteCond, you can say if the 'quote' parameter exists, you'll capture it's value (by getting all characters that follow it that are not '&'). This populates the %1 variable which you can use in the rewrite itself.
I have some urls in my website like this..
http://www.mydomain.com/about.php
http://www.mydomain.com/contact.php
http://www.mydomain.com/signup.php
http://www.mydomain.com/testimonials.php ...... and much more
Now I am trying to convert above urls to user friendly url
http://www.mydomain.com/about
http://www.mydomain.com/contact
http://www.mydomain.com/signup
http://www.mydomain.com/testimonials
This is code in my .htaccess file that I am tried so far. But it doesn't work.
# Add trailing slash to url
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/|#(.*))$
RewriteRule ^(.*)$ $1/ [R=301,L]
# Remove .php-extension from url
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^([^\.]+)/$ $1.php
Anyone can help me to remove .php etension from my urls??
Thank you.
Get rid of the conditions you are using and instead use the below 2 lines in your htaccess file
RewriteEngine On # Turn on the rewriting engine
RewriteRule ^whatever/?$ whatever.php [NC]
This will work even if user types a forward slash after the name or not
If you want to make them dynamic, here's a good tutorial on that
for a proper implementation so as not cause confusion during deployment if you dont have access to the htaccess file of your server or if it is override. You can consider a library like toro-php http://toroweb.org/ for easy routing.
I tried everything I see here in this site to rewrite dynamic url to SEO friendly URL.
Is it maybe because im using it in the localhost?
I try this but does not work also:
RewriteCond %{THE_REQUEST} ^GET\ /index\.php/?([^ ]*)
RewriteRule ^index\.php/?(.*) /$1 [R,L]
RewriteCond $0 !^index\.php($|/)
RewriteRule .* index.php/$0 [L]
I also refer to an online dynamic url generator , but it doesn't work either. Please help.
I would like to rewrite these couple of URLs:
index.php?p=home
index.php?p=about me
index.php?p=contact me
You can use this one:
RewriteEngine on
RewriteBase /
# Rewrite URLs of the form 'x' to the form 'index.php?q=x'.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?q=$1
Then on your index.php page, all the variables will put accessible via $_GET['q']. If you further use $location=array_filter(explode('/',$_GET['q'])); then $location will be an array containing each directory, so www.mysite.com/firstdir/seconddir/thirddir will have $location[0] as 'firstdir' and $location[1] as 'seconddir' and so on. You could then compare these to url_aliases in your database to determine what content/template to display.
This also works for localhost for me, except I change the base url part from "/" to "/mycurrentconstructionsite/"
Hope that helps!