Why htaccess not working for mobile browser? - .htaccess

I have website (mzadkm.com) try to RewriteRule short url to app.php page .
So if user browse "mzadkm.com/app" will show "mzadkm.com/app.php" page
RewriteRule ^/app /app.php [L,R=301]
It's work on Computer , but on mobile browser give me 404 page
Any ideas

That probably is what you are looking for:
RewriteRule ^/?app /app.php [L]
The documentation clearly says, that the pattern in a RewriteRule get's applied to the relative path of the request if the rule is implemented inside a distributed configuration file. That means you actually want to match the path app and not /app here. Which is why your rule did not get applied. The ^/?app is a variant to accept both path notations, relative and absolut, which means the same rule can get implemented in the central configuration or likewise in a distributed configuration file (".htaccess").
I took the liberty to also remove the external redirection you showed ("R=301") since that most likely is not what you want, according to the phrasing of your question. Instead you want an internal rewrite .
You need to take care however that you do not implement a rewriting loop. Which would result in failing requests and an "internal server error" (http status 500).
One approach would be that:
RewriteEngine on
RewriteRule ^/?app$ /app.php [L]
Here another one:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^/?app /app.php [L]
Why things looked fine on your computer, but not on a mobile browser is unclear. Since the same rules get applied and the requests look the same there has to be another reason for that. I suspect you looked at a cached result of a previous attempt somewhere. Remember to always use a fresh anonymous browser window when testing. And to check the response you receive back inside your browsers network console.

Related

htaccess RewriteRule leaving the current directory scope

I'm creating a website based on wordpress on a hosting system (unfortunately not a dedicated system in this situation) and I am very limited in my configuration opportunities for Apache2.4.x / PHP8.
I was unable to convince my client to move to a more advanced environment, so I have to work with the given playground, as follows:
There is a website in /www/ which is linked to the domain (A-Record). There's an old man that maintains the current website until the new website is finished.
I am really not afraid that the old dev accesseses my development scope intentionally or uses PHP to do so to cause harm (he doesn't know PHP, he uploads locally generated HTML). He's an old man and I'm rather afraid that he accidently deletes, overwrites or moves my work while I'm working on the new website and I have to put it back together. He might be like "oh I don't know that folder" and it's gone.
My first task was to make and install certificates and enforce HTTPS, that worked pretty well so far.
Now I need www.domain.tld/dev/ to show the wordpress site, however the old developer can access the www scope and I really don't want him to mess with my code. He barely knows HTML.
In opposite to him, I have full access and can go outside of the /www/ directory, so I created a /wordpress/. Unfortunately I have no option to add a subdomain for that on said host, either.
Now here's where my problem and my approaches kick in, I am unable to move /www/dev/ to show the /wordpress/ content which is not inside of /www/.
Theoretically I would do this on my root server but my client wants the website development to happen in his webspace. That's a bit strange but no subject to change. Just take that as given fact please.
So my htaccess rules for this are not doing anything and I can not spot what's wrong.
(Note: The .htaccess is laying inside of /www/)
RewriteEngine On
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^domain.tld [NC]
RewriteRule ^(.*)$ https://domain.tld%{REQUEST_URI} [R=301,L]
#RewriteBase /
#RewriteRule ^/dev /is/htdocs/wpCUSTOMERIDSTRIPPED/wordpress [QSA]
#RewriteRule ^/dev/(.*) /is/htdocs/wpCUSTOMERIDSTRIPPED/wordpress/$1 [QSA]
RewriteBase /is/htdocs/wpCUSTOMERIDSTRIPPED
RewriteRule ^/www/dev /wordpress [QSA]
RewriteRule ^/www/dev/(.*) /wordpress/$1 [QSA]
Thank you alot in advance.
Note: The search function did not help me further as I am moving outside of the scope of the active directory.

I need to redirect my dynamic URL to a clean and SEO friendly static URL using htaccess

I am a web developer. I have developed a news portal for my client. But the URLs of the articles are dynamic and I need to redirect it to a static URL for SEO purpose.
The current URL : https://example.com/single-post.php?id=1&category=news&title=this-is-a-title
Desired URL : https://example.com/news/this-is-a-title
Someone please help me.
I have wrote this :
RewriteCond %{QUERY_STRING} (?:^|&)id=(\d+)(?:&|$)
RewriteCond %{QUERY_STRING} (?:^|&)title=([^&]+)(?:&|$)
RewriteRule ^/?single-post\.php$ /%2/%1 [R=301]
RewriteRule ^/?(.*)/(\d+)$ single-post.php?title=$1&id=$2 [END]
But the URL output is not what I expected. It is like :
https://example.com/this-is-title/?id=1&title=this-is-title
The only title came first without the id and then the old format came again after the slash. I can't understand what is going on here.
What you ask actually is not possible. There is no way for the rewriting module to somehow magically guess the numerical ID of that object you request. What you can actually do is publish URL in the style of https://example.com/news/1/this-is-a-title. Notice the ID in there, that is what is usally done. For that his should point you into the right direction:
RewriteEngine on
RewriteRule ^/?news/(\d+)/(.*)/?$ /single-post.php?id=$1&category=news&title=$2 [END]
Typically your application logic will only need the numerical ID of the requested object to fetch it from your database. So you typically can silently drop the title in the internal rewriting which makes things even more simple:
RewriteEngine on
RewriteRule ^/?news/(\d+) /single-post.php?id=$1&category=news [END]
In case you receive an internal server error (http status 500) using the rule above then chances are that you operate a very old version of the apache http server. You will see a definite hint to an unsupported [END] flag in your http servers error log file in that case. You can either try to upgrade or use the older [L] flag, it probably will work the same in this situation, though that depends a bit on your setup.
This rule will work likewise in the http servers host configuration or inside a dynamic configuration file (".htaccess" file). Obviously the rewriting module needs to be loaded inside the http server and enabled in the http host. In case you use a dynamic configuration file you need to take care that it's interpretation is enabled at all in the host configuration and that it is located in the host's DOCUMENT_ROOT folder.
And a general remark: you should always prefer to place such rules in the http servers host configuration instead of using dynamic configuration files (".htaccess"). Those dynamic configuration files add complexity, are often a cause of unexpected behavior, hard to debug and they really slow down the http server. They are only provided as a last option for situations where you do not have access to the real http servers host configuration (read: really cheap service providers) or for applications insisting on writing their own rules (which is an obvious security nightmare).
UPDATE:
in your comment to this answer you suggest to also do an explit redirection in case the target URL is used on the client side. Here is a variant of version 2 above which adds that redirection:
RewriteEngine on
RewriteCond %{QUERY_STRING} (?:^|&)id=(\d+)(?:&|$)
RewriteRule ^/?single-post\.php$ /news/%1 [R=301]
RewriteRule ^/?news/(\d+) /single-post.php?id=$1&category=news [END]
A variant of version 1 would look similar:
RewriteEngine on
RewriteCond %{QUERY_STRING} (?:^|&)id=(\d+)(?:&|$)
RewriteCond %{QUERY_STRING} (?:^|&)title=([^&]+)(?:&|$)
RewriteRule ^/?single-post\.php$ /news/%1/%2 [R=301]
RewriteRule ^/?news/(\d+) /single-post.php?id=$1&category=news [END]
Is is a good idea to start with a 302 redirection first. And only change that to a 301 redirection once everything works fine. That saves you from hassles with client side caching while you are still trying things out.

htaccess: act as if files were in another directory, but RewriteRules seem to be mutually exclusive

For files in several subdirectories of /data/, I would like to treat them (and the files inside them) as if they were in the root directory.
So,
1) a request to
/data/foobar/file.png
should redirect the browser to
/foobar/file.png
2) any requests to
/foobar/file.png
should respectively deliver the file /data/foobar/file.png, but without redirection.
For 1) I got the following rule working
:
RewriteCond %{REQUEST_URI} ^(.*)?data/((foobar|and|some|other|subdirs)/.*)$
RewriteRule .* %1%2 [R=301,L,qsappend]
(I took this approach usind a RewriteCond with %x references in order to be subdirectory-agnostic, as in my dev environment the page is located in a subdirectory as opposed to the live system where it's in the root of the domain.)
And for 2) the following rule does the trick:
RewriteRule ^((foobar|and|some|other|subdirs)/.*)$ data/$1 [L,qsappend]
However, these rules only work if I enable one at a time. If I enable both of them at the same time, the browser will abort the request with a "too many redirects" error. The redirect from /data/* to /* will work, but then end in the aborted request just the same as calling the URL without /data/*.
I'm having a hard time understanding why this is happening. It would be totally logical if both rules actually triggered a redirect. But as far as my understanding of htacccess goes (and the Network tab of the dev console seems to confirm that conception), the client shouldn't even know for case 2) that the file is not actually there. So why does this apparently still count towards the redirection limit?
Is there something fundamental I'm missing? And how can I achieve what I'm trying to achieve?
This is because you first redirect and then rewrite the same Uri . Your second rule is conflicting with with the first one.
You need to match against %{THE_REQUEST} instead of %{REQUEST_URI} to avoid the redirect loop
RewriteCond %{THE_REQUEST} \s(.*)?data/((foobar|and|some|other|subdirs)/.*)\s
RewriteRule .* %1%2 [R=301,L,qsappend]
RewriteRule ^((foobar|and|some|other|subdirs)/.*)$ data/$1 [L,qsappend]

htaccess redirect with multiple parameter

I am using following htaccess rewrite for clean URL:
RewriteRule ^([^/]+)/?$ category.php?cat=$1 [L,QSA]
RewriteRule ^([^/]+)/([^/]+)/?$ subcategory.php?cat=$1&subcat=$2 [L,QSA]
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/?$ item.php?cat=$1&subcat=$2&item=$3 [L,QSA]
And I get clean URL like:- domain.com/SHOE/MEN/ITEM-NAME
Now I want change URL to be:- 1. domain.com/BOOTS/MEN/ITEM-NAME
and similar changes at parent level like: 2. domain.com/BOOTS/MEN and 3. domain.com/BOOTS
I tried with one additional line
RewriteRule ^SHOE/.*$ /BOOTS/$1 [L,R]
But still not working. Sorry If I made it complex. Thank you for help in advance.
Your question is not really clear, but I assume you want to redirect incoming browser requests to URLs using BOOTS instead of SHOE, whilst keeping the rest of the requested URL as it is...
For that try something like that:
RewriteEngine on
RewriteRule ^/?SHOE(.*)$ /BOOTS$1 [R=301,END]
That rule should be placed at the beginning of a possible series of rewrite rules. If you are using an old apache http server you might have to use the L flag instead of END. That also means you might have to take additional measures to prevent an endless rewriting loop.
And a general hint: you should always prefer to place such rules inside the http servers host configuration instead of using dynamic configuration files (".htaccess"). Those files are notoriously error prone, hard to debug and they really slow down the server. They are only provided as a last option for situations where you do not have control over the host configuration (read: really cheap hosting service providers) or if you have an application that relies on writing its own rewrite rules (which is an obvious security nightmare).

url rewriting slows down proxy a lot

i have a proxy script which writes very ugly long URL. the proxy script automaticaly rewrites all href links with long ugly URL,
mysite.com/proxy.php?url=somesite.com
mysite.com/proxy.php?url=somesite.com/somedir
mysite.com/proxy.php?url=somesite.com/somedir/somepage.php
to fix this i have done 2 things
edit the proxy script so that all
href links are rewritten as
mysite.com/somesite.com
added a rewrite rule in .htaccess so that short
URLs now rewrite...
mysite.com/somesite.com
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ proxy.php?url=$1 [L,QSA]
the problem is that, whenever i click a link on a proxy loaded page, it seems to take a lot longer than before i did all this cosmetic work. is something wrong with my rewrite rule ?
The test you've written does two file tests (if 'not-file' and 'not-dir') and if they are true, it will execute your RewriteRule. This test is done for every request. Even if the result would've otherwise only required a HTTP 304 (not modified) response or when the actual files are currently held in memory by Apache.
It's possible that this slows down your site significantly if it contains many (server/client cached) images or other resources. To prevent this, try to filter without the need to access your file system. Filter as much as possible by using a regular expression. The easiest way to do this is by changing the URI path part to something unique, for instance example.com/u=somesite.com or even example.com/p/somesite.com (p for proxy)).
Your tests will be much faster if all you need to do is a regular expression pattern match as opposed to two file-existence tests (example for example.com/p/somesite/etc/....):
RewriteCond %{REQUEST_URI} ^/p/
RewriteRule ....
Update: expanded a bit and added small example
PS: even if you do not want to change your current brief URI scheme, you can use this pattern temporarily to test whether it changes performance for the better.

Resources