I have a anchor tag in my html content like this class="list-content" href="/abcd/test.html".
and this is in a lot of places in my html for a list of some results.
I need to append all these URLs that are in "href" by appending a prefix.
For example: /abcd/test.html should be dynamically changed as newprefix/abcd/test.html
If i have another one like /xyz/some.html then this should be changed as newprefix/xyz/some.html
I have explored different solutions over the internet and I have not found something that would fit my problem.
To implement an external redirect to prepend /newprefix to these requests you could do something like the following near the top of the root .htaccess (or server config).
For example:
RewriteEngine On
RewriteRule ^[^/]+/[^./]+\.html$ /newprefix/$0 [R=302,L]
The above will redirect requests for /abcd/test.html or /xyz/some.html to /newprefix/abcd/test.html and /newprefix/xyz/some.html respectively. Anything that matches the pattern /<something>/<file>.html.
$0 is a backreference that contains the URL-path that is matched by the RewriteRule pattern.
Note that this is not "url-rewriting" since you stated in comments that you do not want to "hide" the /newprefix part of the URL from your users. An external redirect is therefore the only solution if you are intending to use Apache / mod_rewrite (as tagged).
Aside: This is not particularly good for SEO, your users or your server since the user is externally redirected everytime they click one of your links, potentially doubling the number of requests that hit your server and slowing your users.
Related
I am trying to redirect a URL to a page with an anchor tag. The redirect works but how do I keep the original URL after the redirect rather than the redirected one.
I want to redirect www.example.com/caves/ to www.example.com/trips.html#caves.
When I redirect I get the URL www.example.com/trips.html#caves but want it to keep www.example.com/caves/.
I have spent ages looking for answers but no luck, any help would be appreciated
Here is my code in htaccess
RewriteEngine on
RewriteRule ^Caves/(.*) /trips.html#Caves [NE,L,R]
I doubt it can be done. Anchor is a client side directive and you can't "hide" it with server side rewrite (the browser can't jump to anchor if it doesn't see one), so the anchor has to be in address bar URL.
...how do I keep the original URL after the redirect rather than the redirected one.
You want an internal rewrite, as opposed to an external redirect - which is what you are currently doing with the use of the R (redirect) flag on the RewriteRule.
RewriteRule ^Caves/(.*) /trips.html#Caves [NE,L,R]
Ordinarily you could simply remove the R flag to keep the original URL in the address bar. The request is internally rewritten to /trips.html.
However, the fragment identifier (ie. #Caves - or strictly everything after the #) is lost. The request is rewritten to /trips.html, not /trips.html#Caves. The fragment identifier is evaluated by the client, not the server and is no doubt being used by your client-side JavaScript to display the appropriate content. Because of the internal rewrite, the client-side JavaScript never gets to see the fragment identifier.
However, if you are internally rewriting the request (ie. /Caves/ is still present in the address bar) then your client-side code doesn't need the #Caves fragment identifier. It can simply look at the original URL, ie. /Caves/ instead. But this will require a small change to your client-side code.
(Aside... Note that you are using Caves (capital first letter) in your code, but referring to caves (all lowercase) in your description. The directive you have written is case-sensitive, so it should be /Caves/, not /caves/ in your description?)
i made a RewriteRule in my .htaccess to redirect the following pattern www.mysite.com/s/1 to www.mysite.com/site/core/gallery.php?s=1, the redirect works but the page loads with missing links how can i fix this
this is my rewrite rule:
RewriteEngine On
RewriteRule ^s/([A-Za-z0-9-]+)/?$ /site/core/gallery.php?s=$1 [NC,L] # Handle scene requests
Most likely what you refer to with the expression "with missing links" is that objects referred to via links inside that page are not loaded once the page itself is loaded. This can have a number of reasons, since you do not provide any details we have to guess...
Most likely the reason is that your "links" are of wrong format, absolute instead of relative. Take a look at the format of the links that are not loaded. Probably they are either full urls including protocol scheme and server name or an absolut path (starting with a /). Since you redirect the request but most likely not requests to those embedded links things don't match.
Have a try changing the links to relative format (not starting with a leading /), so that the referenced objects can be found relative to the pages position in the namespace of the server.
We have created a bunch of landing pages on a Joomla CMS system, such that the URL for each landing page is www.domain.com/page1.html and www.domain.com/page2.html, and so on. Of course the page1.html isn't really an HTML file it is a dynamic CMS page, just rewritten with htaccess.
The goal is to have one of our other domains, something like www.uniquedomain1.com show the content of www.domain.com/page1.html. Or, another domain like www.uniquedomain2.html show the content of www.domain.com/page2.html.
This needs to be search engine friendly so we can't use URL masking. Also we can't use HTACCESS redirects as this actually changes the URL in the browser bar. Need to keep the www.uniquedomain1.com URL in the browser bar.
Tried Apache VirtualHost options without any luck. You can park in a directory but not from a URL.
Ended up parking the domains on one folder, and then creating a PHP script to detect the domain host and then use CURL to query the correct url and deliver content. This whole thing seems ridiculously over complicated, and of course CURL isn't the best option, but it is all we could get to work.
Any thoughts on how to do this, or a better solution?
You can use HTACCESS redirect rules to do it without performing a redirect.
Change the html file names to be the domain name of the desired domain like domain.tld and do something like this in an .htaccess file
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(?:www\.)?([a-z0-9\.-]+\.[a-z]+) [NC]
RewriteRule ^$ /%1.html [L]
A quick test of this worked for two of my test (sub)domains test.domain.tld and test2.domain.tld. Both properly redirected to files with the names test.domain.tld.html and test2.domain.tld.html without modifying the URL.
You could also just use your PHP wrapper script to grab the content of each of the miscellaneous html files and output them.
If you renamed all of your HTML files (as in my previous suggested answer) to be domain.tld.html you could do it fairly easily. Something might look like:
<?php
require($_SERVER['SERVER_NAME'] .'.html');
Say I have a URL like http://abc.com/index.php?cat=37&subsubcat=0&subcat=199&page=product_detail&product_id=1661
Can I use .htaccess to redirect/rewrite this to a URL like http://abc.com/simplerName?
I found quite a few posts on SO that ask for friendly URLs, but I want to take it a step further, i.e. I'd like to specify what the subfolder name should be (in the above example, it's "simplerName"). Now I've got 10 URLs that I want to customize and I'm totally cool with specifying 10 rules for each URL.
But I'm not sure how to achieve this using .htaccess. Is this even possible?
Because you are using non-existing "subfiolders" to indicate which rule should match what, this is going to be pretty straight-forward:
You want something like this:
RewriteEngine On
RewriteRule ^/?category/([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)/ /index.php?cat=$1&subsubcat=$2&subcat=$3&page=$4&product_id=$5 [L]
RewriteRule ^/?something-else/([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)/ /index.php?something=$1&subsub=$2&sub=$3&page=$4&something_id=$5 [L]
etc.
You'll then need to change all the URLs that you serve to the friendly looking ones instead of the ones with the query string.
I'm managing an instance of Wordpress where the URLs are in the following format:
http://www.example.com/example-category/blog-post-permalink/
The blog author did an inconsistent job of adding categories to posts, so while some of them had legitimate categories in their URLS, at least half are "uncategorised".
I can easily change Wordpress to render the URL without the category name (e.g., http://www.example.com/blog-post-permalink/), but I'd like to create a mod_rewrite rule to automatically redirect any requests for the previous format to the new, cleaner one.
How can I use a mod_rewrite recipe to handle this, taking into account that I want to honor requests for the real WordPress directories that are in my webroot?
Something as simple as:
RewriteRule ^/[^/]+/([^/]+)/?$ /$2 [R]
Perhaps would do it?
That simple redirects /foo/bar/ to /bar.