Apache: .htaccess: SEO: how to eliminate query string syntax (PHP) - php

Excellent help for my question is provided in this post. However, despite trying things as described therein I am still having problems because it seems as though my .htaccess file is not being parsed.
I have a website which consists of the following pages:
http://mywebsite.com/?menu1=home
http://mywebsite.com/?menu1=posts&menu2=johndoe
http://mywebsite.com/?menu1=posts&menu2=janedoe
http://mywebsite.com/?menu1=posts&menu2=nickdoe
http://mywebsite.com/?menu1=fashion&menu2=armani
http://mywebsite.com/?menu1=fashion&menu2=gucci
http://mywebsite.com/?menu1=about
The pages are displayed through the default index.php file in my xampp directory on Windows.
What would it take to have the browser respond to the following SEO frienly links instead? I would like to have my PHP code work with at least modifications as possible.
http://mywebsite.com/home
http://mywebsite.com/posts/johndoe
http://mywebsite.com/posts/janedoe
http://mywebsite.com/posts/nickdoe
http://mywebsite.com/fashion/armani
http://mywebsite.com/fashion/gucci
http://mywebsite.com/about
Thanks.

You can use these rules in your Root/.htaccess
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/([^/]+)/?$ /?menu1=$1&menu2=$2 [QSA,NC,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/?$ /?menu1=$1 [QSA,NC,L]
The first rule rewrites "/posts/jondoe/" to "/?menu=posts&menu=jonde" and the second rule rewrites "/about" to "/?home=about"..

Related

Clean url rewriting using .htacces file in apache

I have a website where i do blogging... on my blog.php page i have posts from different categories.
when a user clicks on a link it is being redirect to /blog-details.php page with $_GET['title'] variable.
what I have tried
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
#remove .php from all pages
RewriteRule ^([^\.]+)$ $1.php [NC]
#blog/title-here
RewriteRule ^blog/([A-Za-z0-9-\+]+)/?$ /blog-details.php?title=$1 [NC,L,QSA]
last line gives me something like
xyz.com/blog-details?title=hello-world
what i want to achieve is
xyz.com/blog/hello-world
not like
xyz.com/blog-details?title=hello-world
thank you, looking forward to answers.
An important thing you have to take into consideration when working with .htaccess files, is that you have to check if the path is a directory or file, then proceed to do your action.
I used your code and it worked properly, but simply added the -f and -d checks to make sure that this does not affect other pages.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
#remove .php from all pages
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^\.]+)$ $1.php [NC]
#blog/title-here
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^blog/([A-Za-z0-9-\+]+)/?$ /blog-details.php?title=$1 [NC,L,QSA]
To test out your .htaccess file and with some dummy URLs, I'd highly recommend you to check out MadeWithLove
https://htaccess.madewithlove.be/
This will let you have a test input URL and see if you get the proper output URL (the actual URL your server would read).
With that being said, I would recommend you to look into routing for future projects and self-development. A framework I'd recommend checking out is fat-free framework (f3) for PHP. It's very easy to get into, you could learn it in 2 days, and it saves a lot of time in development - by containing routing, ORM, global variables, simplified SQL queries, and plenty of other features that essentially keep you a PHP developer, rather than a framework developer.
use below method:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^blog/([0-9a-zA-Z_=-]+)$ blog-details.php?title=$1 [NC]
</IfModule>
Then you can access the title like $_GET['title'] on the page.

How rewrite TWO URLS for .HTACCESS?

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.

htaccess rewrite subdirectoy to file

I have a problem with rewriting urls to my files. What I am trying to do is making my little shop system a bit more SEO friendly. My problem is that it sometimes works and sometimes it doesn't. I have no idea what I should do or what I am doing wrong.
My UPDATED .htaccess file:
ErrorDocument 404 /shop/404.html
RewriteEngine On
RewriteBase /shop/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^produkte/?(.*)$ products.php$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^produkt/?(.*)$ product.php?url=$1 [L]
RewriteRule ^(.*)/(css|js|img|fonts)/(.*)?$ /shop/$2/$3 [L,QSA,R=301]
For example the /shop/products link is not working but /shop/products/ is.
And for some reason if I want to open the link /shop/products?cat=besteck its redirecting me to: localhost/D:/xampp/htdocs/shop/products.php?cat=besteck but If I capitalize the b it's working fine..
I have no Idea what to do, please help me! (And dont just give a working code snippet explain why mine fails and yours works)
EDIT:
Just to clear things up I want /products, /products/ and /products?some_get_query to redirect to my products.php file. /product/some_seo_url should be redirected to product.php?url=some_seo_url. I tried adding a question mark after the forward slash in my RewriteRule and I also tried putting the ^products rule above the ^product rule. Nothing worked yet.
EDIT 2:
I updated my .htaccess code above and now nearly everything works. The only thing that still doesn't work is when I open /shop/products/?cat=fish or /shop/product/some_product, my resources aren't loading!
ErrorDocument 404 /shop/404.html
RewriteEngine On
RewriteBase /shop/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^products/?(.*)$ products.php?$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^product/?(.*)$ product.php?url=$1 [L]
RewriteRule ^(.*)/(css|js|img|fonts)/(.*)?$ /shop/$2/$3 [L,QSA,R=301]
First off your product rule also matches products so a rewrite like:
products/cat/fish becomes product.php?url=s/cat/fish
Which is not what you want, the easiest way to avoid that is to reverse the order so that the products rewrite comes before the product one but I've also added the Last flag ([L]) to be on the safe side; besides, once it's got the match you want it's better for it to stop looking.
To prevent recursive rewrite loops you need to specify that the rewrite only occurs when the redirect is not an existing file or directory (otherwise your product rewrite matches product.php and it loops - forever). That's what those RewriteCond lines signify.
Other than that it seems OK.

RewriteRule to redirect to php file not working

I just want a simple redirect to clean up the url's on a site.
e.g.
I want ajhtestserver.com/registration/ to redirect to ajhtestserver.com/registration.php
It should be easy and I have successfully used .htaccess rewrites on other sites but for some reason it just will not work for me today.
RewriteEngine On # Turn on the rewriting engine
RewriteRule ^registration[/]$ registration.php [NC,L] # Handle requests for "registration"
I am sure it is something simple that I am missing but I basically just copied what I have on other sites that work fine for me so I am confused as to why it just refuses to work for me here (gives me The requested URL /ajhtestserver/registration/ was not found on this server. error). Just one of those days :(
Any help is appreciated.
Thanks,
Adam
if you use apache ,first you should enable rewrite_mode in http.conf or ...\
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^registration/(.*)$ registration.php/$1 [L]
check .htaccess syntax or rewrite mode.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)[/]$ $1.php [L]
Well it didn't seem to like it when the redirect source word and target filename were the same word but this works...
RewriteRule ^([a-zA-Z\ ]+)[/]?$ $1.php [NC,L]
And that is actually a better solution anyway as it doesn't require a separate rule for each page.
Though I never did figure out why it didn't like it the original way.

How can I redirect every URL that's not requesting an JPEG or PNG image to index.php/x?

For example, I have an URL that looks for an image like this:
http://example.com/img/foo.png
http://example.com/img/interface/menu/bar.png
http://example.com/static/users/avatars/small/3k5jd355swrx221.jpg
I don't want to redirect those. They should just pass through. But then, I have URLs like this:
http://example.com/register/
http://example.com/my_account/my_picture/
http://example.com/contact/email/
All such URLs that don't request for an .png or .jpeg should be redirected to:
http://example.com/index.php/x
Where x stands for everything after example.com/, so in this example for example:
http://example.com/register/ to
http://example.com/index.php/register/
http://example.com/my_account/my_picture/ to
http://example.com/index.php/my_account/my_picture/
http://example.com/contact/email/ to
http://example.com/index.php/contact/email/
(AcceptPathInfo is enabled)
Is there any way to do that in the .htaccess? I only know how I could do this if I had always something like http://example.com/someKindOfMarkerHere/stuff/stuff/stuff but I don't want to have the someKindOfMarker there to detect if it's an URL that has to be rewritten. I don't know how to exclude them.
You can either exclude specific URLs:
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/index\.php$
RewriteRule !.*\.(jpeg|png)$ index.php%{REQUEST_URI}
Or you exclude any existing file:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php%{REQUEST_URI}
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} ^.+\.png$ [OR]
RewriteCond %{REQUEST_FILENAME} ^.+\.jp(e)?g$
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php/%{REQUEST_URI} [NC,L]
Hell yes it's possible.
mod_rewrite will do all of that for you pretty easily.
You can also set up an error handler, so every 404 on your site gets redirected through index.php. This is a nice little way of making sure all requests load index.php (or your bootstrap).
The mod_rewrite will need a regex and regex's hurt my head, so I'll let somebody else write one.
Hope that helps. Just comment if you need more info from me. :)
Put something like this in a .htaccess file and make sure mod_rewrite is enabled:
RewriteEngine On
RewriteRule ^(.*?)(?!(\.png|\.jpg))$ index.php/$1
http://www.regular-expressions.info/lookaround.html
I would use a slight variation to Gumbo's answer:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php%{REQUEST_URI}
It excludes folders as well as files (the !-d flag) - you may not may not want this, but think it belongs here for completeness.
The following ignores existing files, folder and files with the extension matching: jpg, jpeg, png, gif. If you wish to add additional extension, just add "|extension" before the )$ on line 3.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !.(jpg|jpeg|png|gif)$
RewriteRule ^(.*)?$ /index.php/$1 [QSA,L]

Categories