url rewriting pattern is not working - .htaccess

my current application is using codeigniter, where a method will output will deliver javascript code which can be included in one of my other domain site.
my requested url will be like
http://domain.com/widget/ID.js
will be re-written as
http://domain.com/home/widget/ID
here is my code but not working
RewriteEngine on
RewriteRule ^widget/([a-z0-9-]+)\.js/$ home/widget/$1 [NC]
can any one help me with that thank you.

If you're wanting to rewrite /widget/ID.js to /home/widget/ID, then your original rule was correct (it was not backwards). It probably just requires some fine tuning. Assuming you can successfully hit the rewrite target URL on its own?
http://domain.com/home/widget/ID
Then I would try adding leading slashes to both your pattern and sub and removing the trailing slash in your pattern:
RewriteRule ^/widget/([a-z0-9-]+).js$ /home/widget/$1 [NC]
Also, make sure you've enabled the rewrite module:
LoadModule rewrite_module modules/mod_rewrite.so

Related

htaccess block original file however allow rewrite

I have the following entries in my /.htaccess
RewriteEngine On
RewriteBase /
RewriteRule ^signin.php - [L,gone]
RewriteRule ^sign-in/?$ /signin.php [L]
What I am trying to achieve is to allow the user to access /sign-in but not the original file /signin.php
I saw this other question however when I tried to implement the answer I get gone error on both /sign-in and /signin.php.
Referenced Question:
Alias within htaccess, and block access to original file? (URL rewriting)
Thanks for any help.
To redirect any direct requests for /signin.php to /sign-in and internally rewrite /sign-in back to /signin.php then you can do it like this:
RewriteEngine On
RewriteRule ^signin\.php$ /sign-in [R=301,L]
RewriteRule ^sign-in$ signin.php [END]
The END flag (Apache 2.4) prevents any further loops by the rewrite engine, so prevents the rewritten URL being redirected back to /sign-in, which would otherwise cause a redirect loop.
The END flag is not required on the first rule, since an external redirect will trigger immediately. (The L flag is still required.)
You do not need the RewriteBase directive here.
I removed the optional trailing slash on /sign-in as this potentially creates a duplicate content issue. If you wish to allow an optional trailing slash then redirect to the canonical URL (without the trailing slash, or with if you prefer) instead.
UPDATE:
I saw this other question however when I tried to implement the answer I get gone error on both /sign-in and /signin.php.
Referenced Question:
Alias within htaccess, and block access to original file? (URL rewriting)
The Apache solution in the referenced answer is not quite correct. The answer would seem to have been "accepted" for the first part regarding doing this in PHP instead.
At the time of that question (2011), they would have been using Apache 2.2. The END flag was only introduced in Apache 2.4.
They would have needed to have done it like this, to prevent a redirect loop:
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^about.php /about [L,R=301]
RewriteRule ^about$ about.php [L]

how to rewrite SEO URL

I am just new to .htaccess.
I need some rewrite rules for URLs.
I Google'd some and applied but no change in URL.
I want:
demo.example.com/files/section.php?id=1
Changed to:
demo.example.com/sample-section
I tried
Since you use .htaccess I assume you are using Apache. Here you'll find all relevant documentation.
First of all you need the mod_rewrite module to be installed (instructions to do so depend on the server's operating system and Apache distribution).
Then, the URL rewrite is pretty simple:
# First of all tell to mod_rewrite to operate.
RewriteEngine on
# Then, as many times you need, tell it on what to operate...
# For example: on files that do not exist. Or leave out RewriteCond to act on all.
RewriteCond "%{REQUEST_FILENAME}" !-f
# ...and what to do.
RewriteRule /sample-section "/files/section.php?id=1" [PT]
RewriteRule /another-section "/files/section.php?id=2" [PT]
The PT (PassThru) flag might be needed in some contexts, otherwise just use [L].

Why doesn't this URL rewrite work?

This is a page on my domain: www.mydomain.com/en/stats.php
I want it to look like this: www.mydomain.com/en/statistics/players
This is pretty simple to accomplish, but for some reason the htaccess code below doesn't work.
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^/statistics/players stats.php [R=301,L,QSA]
I get a 404 error when I try to open the SEO-friendly version. The page opens fine when I use the regular URL. What am I doing wrong?
The URL might not have a leading slash. Try without or optional slash. Additionally, you must check for the leading en, when you anchor your pattern at the beginning
RewriteRule ^/?en/statistics/players /en/stats.php
What worked in the end is the following:
RewriteRule en/statistics/players en/stats.php [NC,L]
I think it needs to be:
RewriteEngine On
RewriteRule ^en/statistics/players en/stats.php [QSA]
The R=301 means it will perform a redirect instead of a rewrite and try to redirect to /stats.php which doesnt exists I guess.
The L flag means that this is the last rewrite rule that will be processed for this request, which in this case I doubt is what you want. If there are rewrite rules further down for /en/stats.php they wont be processed.

trouble with simple mod_rewrite redirect rule

I have mod_rewrite working in a development environment.
This testing domain is using these rules in an .htaccess file:
Options +FollowSymLinks
Options +Indexes
RewriteEngine on
# deal with potential pre-rewrite spidered / bookmarked urls
RewriteRule ^clothes/index.php?pg=([0-9]+)$ /clothes/index$1.php [R=301,L]
# deal with actual urls
RewriteRule ^clothes/[0-9a-z-]+-pr([0-9]+).php$ /clothes/product.php?pid=$1 [L]
The 2nd Rule works fine. Entering http ://testdomain.dev/clothes/shirt-pr32.php is silently delivered content from http ://testdomain.dev/clothes/product.php?pid=32 ...which is as desired and expected!
However, assuming this was applied to a live site, one that had originally used paths such as: http ://testdomain.dev/clothes/product.php?pid=32, I'd like to redirect any incoming requests following the old pattern to the new urls ...which is what the 1st Rule was intended to do.
My problem is my testing server seems to ignore the 1st Rule and serves the page as requested (page loads but address bar remains at http ://testdomain.dev/clothes/product.php?pid=32)
Any assistance or enlightenment would be most graciously accepted!
You need to match the query string within a RewriteCond, then backreference that RewriteCond from the rule. The RewriteRule only matches against the path, not the query string.
Here's a related post I previously answered with a similar request: Mod_rewrite rewrite example.com/page.php?v1=abc&v2=def to example.com/abc/def
You can't match against the query string in a rewrite rule, you need to use the `%{QUERY_STRING} variable in a condition and use the % to backrefernce groupings. So instead of:
RewriteRule ^clothes/index.php?pg=([0-9]+)$ /clothes/index$1.php [R=301,L]
You'll need:
RewriteCond %{QUERY_STRING} ^pg=([0-9]+)
RewriteRule ^clothes/index.php$ /clothes/index%1.php? [R=301,L]

My url rewrite redirects to mydomain.com/var/www/clients/client3/web25/

I'm trying to make a rewrite of a url on my joomla site. I have an extra page in there wiht some other stuff on it.
I have:
RewriteRule ^company/([^/]+) company?alias=$1 [NC]
where i want company/somecompany to show what the url company?alias=somecompany will give (not forward to it).
What i get instead is a redirection to:
http://www.mydomain.dk/var/www/clients/client2/web5/web/company?alias=somecompany
Also if i include a "-" like "company/some-company" it skips my rewrite and just goes to the joomla rewrite rules (and can't find the article)
What am I doing wrong?
You need to add a RewriteBase. When your rewrite rule's target doesn't start with a leading slash (making it an absolute URI), apache needs to guess whether it's a URI-path or a file-path, and it is incorrectly guessing that it's a file-path. Adding a base tells apache that it's a URI path.
RewriteBase /
or add a leading slash to your target: /company/?alias=$1
You also want to add a trailing slash to company and use the [L] flag:
RewriteRule ^company/([^/]+) company/?alias=$1 [L,NC]
The missing trailing slash may be the culprit that's causing mod_dir and DirectorySlash redirecting. And the L flag may be why the joomla rules are eventually getting applied.
Here is the working example:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /
RewriteRule ^/?old-folder/(.*)$ /new-folder/$1 [R=301,L]
</IfModule>
For more examples, check:
Learn Apache mod_rewrite: 13 Real-world Examples
Apache Conf Snippets for VS Code

Resources