how to rewrite joomla pagination to friendly url with htaccess - .htaccess

I want to rewrite pagination URLs http://mydomain.com/category?start=15 to http://mydomain.com/category/start/15, but I don't know how to achieve this with .htaccess.
PS: those URls ( http://mydomain.com/category?start=15) are referenced in search engines so I need to add 301 redirect to my new URLs.
I want mention that I already enabled SEF mode in Jommla configuration

First, if you want your URLs to look like /start/15 you have to rewrite them from the pretty format to the internal, not otherwise.
I currently have no access to an Apache, but your expression should look something simliar to this:
RewriteRule ^category/start/([0-9]+)$ http://mydomain.com/category?start=$1 [QSA,L]
Then, it would be neccesary to tell Joomla how to generate the pretty URLs, this happens without the htaccess file, for example in a SEO plugin.
Finally, to output 301 messages, you will want to add "Redirect" commands to your .htaccess file. Redirect incoming ?start=15 URLs to the pretty format. Make sure that this does not happen after your Rewrite...

Related

Update Apache rewrite rule in .htacess that will still allow for previous URLs to work

I'm working on updating an .htaccess file to update some URLs for an existing website.
Right now, the URLs look like:
nickwebsite.com/nicks-widgets/product-type
I'm trying to reduce the redundancy, to have the URLs look like:
nickwebsite.com/widgets/product-type
The RewriteRules in place now look like this:
RewriteRule ^nicks-widgets/product-type(.*)$ collection.php?category_id=64&%{QUERY_STRING} [NC]
I need to update these URLs to use the new URL, while still keeping the old URLS functional
I tried adding a Rewrite rule like so:
RewriteRule ^widgets/product-type(.*)$ collection.php?category_id=64&%{QUERY_STRING} [NC]
And also a Redirect like this, so that visitors who have the old URL saved will be redirected to the new URL:
Redirect 301 /nicks-widgets /widgets
But neither really work. Instead I'm seeing my URL combined when trying to access the new url, so I will get:nickwebsite.com/widgetsproduct-type?category_id=64 displayed in my browser. I'm really lost on what I am missing.. can anyone point me in the right direction?

.htaccess rewriting URL to add a character to a URL

I'm doing this to fix an error with my AMP pages. I'm using the Automatic AMP plugin, and this plugin lets you access the AMP pages using 2 different methods
site.com/post/amp/
site.com/post/?amp
Using the AMP Page validator (https://validator.ampproject.org) I see that all AMP pages using just /amp/ get multiple errors, while /?amp is validated correctly.
Unfortunately, Google is checking /amp/ for all my pages, hence I'm getting tons of errors.
What I'd like to know is how to use the .htaccess redirect rule to add the ? to the AMP queries so all /amp/ requests are redirected (with a 301) to /?amp/
I'd appreciate suggestions on this. Thank you
Something like this:
RewriteEngine on
RewriteRule ^(.*)/amp/$ /$1?amp [R=301,L]
This will redirect any URL ending in /amp/ to the same but with ?amp instead, which seems to be what you want. To go in your root .htaccess file.

htaccess Redirect URL of Subdirectories managed through CMS

All the urls of our site are written www.mydomain.com/sub1/cms/pagename
Through the CMS we can specify pagename
sub1 and cms are not actually directories on the server.
We need to change the pagenames to be more SEO friendly, but we still want any traffic to the old pagename to direct to the new one.
I have tried making an .htaccess file specifying:
Redirect 301 /sub1/cms/oldpagename mydomain.com/sub1/cms/newpagename
but it does not work.
I have been successful with:
Redirect 301 /oldpagename mydomain.com/sub1/cms/newpagename
Can this be done somehow through .htaccess?
First, make sure your 301 redirect rules for redirect use a full url including the http:// portion.
Redirect 301 /sub1/cms/oldpagename http://mydomain.com/sub1/cms/newpagename
Second, it sounds to me like your CMS has put a rewrite rule in place to handle the /sub1/cms/pagename style urls. If this rule comes before your redirect, your redirect will never get triggered. Try moving your redirect to the top of the .htaccess file.

301 Redirect vs Rewrite

I have a site that was hosted by someone else all the web pages were .html files. I am now hosting the site and have changed it to a wordpress site. The domain has not changed but obviously all the pages have. What is the best way to redirect all the .html pages to the main url?
301 Redirect in an .htaccess does not require the mod_rewrite library. It's a much simpler way to redirect, but it doesn't have the flexibility and power you get using the Rewrite rules. If you have a 1-1 mapping with explicit urls you can use the Redirect:
Redirect 301 /path/file.html http://new.site.com/newpath.php
If you're trying to do wild card matching of a number of similar patterns using regular expressions you'll need to use Rewrite.
RewriteRule ^(.*).html$ http://new.site.com/$1.php [R=301,NC,L]
Here's a pretty good overview of the 2 methods: http://www.ksl-consulting.co.uk/301-redirect-examples.html
There is also RedirectMatch that also does wild card matching of similar patterns using regular expressions. The choice depends on just what you need to do.
Rewrite is complex - learning curve - but you can serve alternative urls without giving a HTML code and things that seem imposible. But with great power comes complexity and lots of bugs.
If you are only doing a simple redirection - possibly matching some urls - redirect is the way to go.
When you can't do it with Redirect, you will probably want to start learning Mod_Rewrite.

Does mod_rewrite only translate external requests to internal files and not vice versa?

I think this is a very stupid question so I apologise, as i think i may completely misunderstand mod_rewrite.
Say you have a URL
www.domain.com/products/item.php?id=1234
mod_rewrite can rewrite that to a friendly URL
wwww.domain.com/products/item/1234
(for example)
So, if i type in wwww.domain.com/products/item/1234 this will be rewritten to www.domain.com/products/item.php?id=1234 and that page is served. Fine.
But what if you type in www.domain.com/products/item.php?id=1234 - that page will be served but not rewritten to the friendly URL.
So my question is can you rewrite internal file names automatically? For example, all URLs on my site are currently in the www.domain.com/products/item.php?id=1234 format. When a user clicks this link can this be rewritten to the friendly URL? Or should you always hard code in the friendly URL?
Im sorry if that made little sense! Im getting confused because i want to rewrite non-friendly to friendly URL, but then serve the non friendly URL - so wont that cause an infinite redirect loop?
Mod_rewrite can't really internally rewrite URLs across domains, though it could proxy them (using P option in RewriteRule). Assuming that the domain is the same, you could do something to redirect the client's browser to a friendly URL if the old one is used while internally rewriting the friendly URL back to the old one, but they have to be both the same domain. You do this by looking at the actual request (%{THE_REQUEST}) variable instead of looking at the URI, which changes as they get rewritten internally.
This redirects the browser when the old URLs are used to the friendly URLs
RewriteCond %{THE_REQUEST} ^([A-Z]{3,9})\ /products/item\.php
RewriteCond %{QUERY_STRING} id=([0-9]+)
RewriteRule ^products/item\.php$ /products/item/%1? [R=301,L]
This rewrites internally when a friendly URL is used:
RewriteCond %{THE_REQUEST} ^([A-Z]{3,9})\ /products/item/[0-9]+
RewriteRule ^products/item/([0-9]+) /products/item.php?id=$1 [QSA,L]
Mod_rewrite does not automatically rewrite the "none friendly" urls to the friendly urls. You have to add some rules yourself to do this.
Also Mod_rewrite does not modify the links inside your html, css, or whatever you use. You need to change those yourself.
If a user uses the friendly url, it will never know that it is rewritten. Mod_rewrite is tranparent from the user's point of view. You can add a [R] flag to your rules which makes apache send a redirect to the client. This way the client does see the rewritten url.
Redirecting the unfriendly to the fiendly url, should only be done to help search engines (and to prevent link-rot, but that's more rare). This can be done without a redirect loop, unlike Sergey says.
Try looking around here on SO to find a script that does the redirect from the unfriendly to the fiendly url. Let me know if you can't find it, and I'll help.

Resources